Android:自定义通知外观问题

标签 android android-notifications remoteview

我在我的应用程序中使用MediaPlayer。我在我的应用中添加了带有媒体播放器控件(例如上一个、下一个和停止按钮)的持续通知,以便用户无需进入应用即可访问这些控件。它在 4.x 平台上看起来不错。这是 Android 4.2.2 上的通知屏幕截图。

Notification on Android 4.2.2

但是,在 Android 2.2 上,它看起来像这样:

Notification on Android 2.2

我的代码如下:

private Notification generateNotification() {
    Log.d(TAG, "generateNotification called");

    Intent actionIntent = new Intent(getApplicationContext(),
            AartiActivity.class);
    PendingIntent pi = PendingIntent.getActivity(getApplicationContext(),
            0, actionIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    RemoteViews mNotificationView = new RemoteViews(getPackageName(),
            R.layout.notification_view);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(
            getApplicationContext());
    builder.setSmallIcon(R.drawable.icon);
    builder.setContent(mNotificationView);
    builder.setOngoing(true);
    builder.setTicker("Aarti Sangrah");
    builder.setContentIntent(pi);
    mNotificationView.setImageViewResource(R.id.imgAppIc, R.drawable.icon);
    mNotificationView.setTextViewText(R.id.txtAartiPlaying, mediaName);

    return builder.build();
}// generateNotification

然后,我在 onPrepared() 中调用了 startForeground(1,generateNotification()); .

Remote Views从 API 级别 1 开始就可用。而且,它也得到了很好的支持。我在某处读到,在 Honeycomb 之前不支持它。但是,在一些搭载 Android 2.x 的设备上,此功能可用。另外,为了检查这一点,我查看了Android 2.2的音乐播放器的源代码here

这里是 Music Player Service 的 fragment .

RemoteViews views = new RemoteViews(getPackageName(),
                R.layout.statusbar);
        views.setOnClickPendingIntent(R.id.btnTest, PendingIntent
                .getActivity(getApplicationContext(), 0,
                        new Intent(getApplicationContext(),
                                MusicBrowserActivity.class),
                        PendingIntent.FLAG_UPDATE_CURRENT));
        views.setImageViewResource(R.id.icon,
                R.drawable.stat_notify_musicplayer);
        if (getAudioId() < 0) {
            // streaming
            views.setTextViewText(R.id.trackname, getPath());
            views.setTextViewText(R.id.artistalbum, null);
        } else {
            String artist = getArtistName();
            views.setTextViewText(R.id.trackname, getTrackName());
            if (artist == null || artist.equals(MediaStore.UNKNOWN_STRING)) {
                artist = getString(R.string.unknown_artist_name);
            }
            String album = getAlbumName();
            if (album == null || album.equals(MediaStore.UNKNOWN_STRING)) {
                album = getString(R.string.unknown_album_name);
            }

            views.setTextViewText(
                    R.id.artistalbum,
                    getString(R.string.notification_artist_album, artist,
                            album));
        }

        Notification status = new Notification();
        status.contentView = views;
        status.flags |= Notification.FLAG_ONGOING_EVENT;
        status.icon = R.drawable.stat_notify_musicplayer;
        status.contentIntent = PendingIntent.getActivity(this, 0,
                new Intent("com.android.music.PLAYBACK_VIEWER")
                        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK), 0);
        startForeground(PLAYBACKSERVICE_STATUS, status);

此代码中使用了远程 View 。它有两个 TextView。我通过添加一个按钮来修改代码,并在单击按钮时执行操作。在每个平台上一切都运行良好。

同样的事情,我想要我的应用程序。但是,在 2.2 上,它看起来就像我上面的屏幕截图所示。我认为这是因为文本和按钮是白色的,所以尝试更改按钮和文本的颜色,但没有运气。据我了解,我唯一想到的是远程 View 在 Android 2.2 上并没有膨胀(就我而言)。我不明白为什么通知在 Android 2.x 平台上没有正确显示。

最佳答案

我解决了我的问题。这是我的解决方案。

private Notification generateNotification() {
        Log.d(TAG, "generateNotification called");

        Intent actionIntent = new Intent(getApplicationContext(),
                AartiActivity.class);
        PendingIntent pi = PendingIntent.getActivity(getApplicationContext(),
                0, actionIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        RemoteViews mNotificationView = new RemoteViews(getPackageName(),
                R.layout.notification_view);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            NotificationCompat.Builder builder = new NotificationCompat.Builder(
                    getApplicationContext());
            builder.setSmallIcon(R.drawable.icon);
            builder.setContent(mNotificationView);
            builder.setOngoing(true);
            builder.setTicker("Aarti Sangrah");
            builder.setContentIntent(pi);
            mNotificationView.setImageViewResource(R.id.imgAppIc,
                    R.drawable.icon);
            mNotificationView.setTextViewText(R.id.txtAartiPlaying, mediaName);
            mNotificationView.setTextColor(R.id.txtAartiPlaying, getResources()
                    .getColor(android.R.color.holo_orange_light));

            return builder.build();
        } else {
            mNotificationView.setTextViewText(R.id.txtAartiPlaying, mediaName);
            mNotificationView.setTextColor(R.id.txtAartiPlaying, getResources()
                    .getColor(android.R.color.holo_orange_light));
            Notification statusNotification = new Notification();
            statusNotification.contentView = mNotificationView;
            statusNotification.flags |= Notification.FLAG_ONGOING_EVENT;
            statusNotification.icon = R.drawable.icon;
            statusNotification.contentIntent = PendingIntent.getActivity(this,
                    0, new Intent(getApplicationContext(), AartiActivity.class)
                            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK), 0);
            return statusNotification;
        }
    }// generateNotification

但是,我很惊讶。 NotificationCompat.Builder在支持包中可用,并且提供它是为了向后兼容。但是,就我而言,它仅适用于 Honeycomb 及以上版本。根据此section :

Not all notification features are available for a particular version, even though the methods to set them are in the support library class NotificationCompat.Builder. For example, action buttons, which depend on expanded notifications, only appear on Android 4.1 and higher, because expanded notifications themselves are only available on Android 4.1 and higher.

但是,我认为这不适用于我的情况。 NotificationCompat.Builder 适用于远程 View ,但不适用于 2.x(至少就我而言,可能是我错了或遗漏了某些内容)。如果有人有这方面的任何信息或资源,请分享,以便我找出错误。

关于Android:自定义通知外观问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18055721/

相关文章:

android - Android设计支持库-版本号和Gradle错误

android - 更新 AppWidget 后未调用 RemoteViewsFactory 上的 onDataSetChanged()

java - 为什么要同时使用 Intent.FLAG_ACTIVITY_NEW_TASK 和 Intent.FLAG_ACTIVITY_SINGLE_TOP?

java - 如何在android中应用onPreferenceClick条件?

Java - 无法连接到服务器 - 连接被拒绝

安卓通知点击恢复 Activity

android - 如何在 NotificationListenerService 中检索 PendingIntent

android - 将 Android 通知从 FLAG_ONGOING_EVENT 切换为可取消

java - 将字符串转换为颜色 int

android - 在应用程序小部件 GridView 中保留图像纵横比