【问题标题】:Android: Custom notification appearance issueAndroid:自定义通知外观问题
【发布时间】:2013-08-05 10:17:36
【问题描述】:

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

但是,在 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 的设备上,此功能可用。另外,为了检查这一点,我查看了来自here 的Android 2.2 音乐播放器的源代码。

这里是来自其Music Player Service 的sn-p。

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);

此代码中使用了远程视图。它有两个TextViews。我通过添加一个按钮来修改代码,并在按钮单击时执行操作。在每个平台上一切正常。

同样的事情,我想要我的应用程序。但是,在 2.2 上,它看起来就像我在上面的屏幕截图中显示的那样。我认为这是因为白色文本和按钮所以尝试改变按钮和文本的颜色,但没有运气。据我了解,我唯一发现的是远程视图在 Android 2.2 上没有被夸大(在我的情况下)。我不明白为什么通知没有在 Android 2.x 平台上正确显示。

【问题讨论】:

    标签: android android-notifications remoteview


    【解决方案1】:

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

    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

    并非所有通知功能都适用于特定版本,即使设置它们的方法位于支持库类 NotificationCompat.Builder 中。例如,依赖于扩展通知的操作按钮仅在 Android 4.1 及更高版本上出现,因为扩展通知本身仅在 Android 4.1 及更高版本上可用。

    但是,我认为这不适用于我的情况。 NotificationCompat.Builder 适用于远程视图,但不适用于 2.x(至少在我的情况下,可能是我错了或遗漏了什么)。如果有人有这方面的任何信息或资源,请分享,以便我找出我的错误。

    【讨论】:

    • NotificationCompat 帮助您构建适用于所有 API 级别的通知,但这并不意味着它允许您在旧版本上使用新功能。旧版本无法处理自定义布局或按钮,因此在实践中您需要定义 3 种布局:通知已扩展,新通知未扩展(对于您还不能扩展的 API 级别以及用户或系统收缩通知时)和旧通知(只是文本)。当然,这取决于您的最低 API 级别。如果您决定使用 minsdk = 14 (ICS),则无需考虑旧版本的通知。
    • @Nitish 给了我一个建议。我使用 RemoteView 构建了一个通知大视图来控制播放/暂停,就像这个链接 (stackoverflow.com/questions/14508369/…) 一切都是正确的,但是当我单击设备后退按钮并从应用程序单击事件中退出时(播放/暂停/转发/关闭)按钮没有不行。请帮帮我。
    • @HelalKhan 嗨,请在 Stackoverflow 上创建一个新问题。不看代码,很难说是什么原因。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多