使用Android L Notification.MediaStyle添加终止播放按钮 [英] Adding a terminate playback button using Android L Notification.MediaStyle

查看:222
本文介绍了使用Android L Notification.MediaStyle添加终止播放按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用新的Android L MediaStyle模板创建媒体播放通知.现在,通过使用addAction(),我成功完成了上一个,播放,暂停,下一个动作的完美操作,但是我找不到在

I want to create a Media Playback notification using the new Android L MediaStyle template. Now, I succeeded doing so perfectly well for actions like previous, play, pause, next (by using addAction(), but I couldn't find a way to add a "close" button like in the Android Notifications Documentation screenshot:

是否有一种巧妙的方法来实现这一目标?我希望关闭"按钮可以终止当前正在播放的播放,清除播放通知,并位于所附的屏幕快照中.

Is there a neat way to achieve this? I want the "close" button to terminate the currently playing playback, clear the playback notification, and be located as in the attached screenshot.

推荐答案

更新

使用此通知样式的支持库版本(即NotificationCompat.MediaStyle)时,会有

When using the Support Library's version of this notification style, i.e. NotificationCompat.MediaStyle, there is the setShowCancelButton() button.

这将添加关闭按钮(,但仅适用于Lollipop之前的版本),以解决包含在作为前台服务的一部分显示后无法取消的通知的错误.

This will add the close button, but only on versions prior to Lollipop, to work around a bug with notifications that cannot be made dismissable after being displayed as part of a foreground service.

在Lollipop中,首选的选择是仅能够取消通知(在音频暂停时),而不是使用自定义按钮来做到这一点.

In Lollipop the preferred option is to just being able to dismiss the notification (when the audio is paused) instead of having a custom button to do that.

旧答案

通过查看Notification.MediaStyle类的源代码,似乎目前不支持MediaStyle通知样式的此类按钮:

From taking a look at the source code of Notification.MediaStyle class it would seem that there is currently no support for such a button in the MediaStyle notification style:

    private RemoteViews makeMediaBigContentView() {
        final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
        RemoteViews big = mBuilder.applyStandardTemplate(getBigLayoutResource(actionCount),
                false /* hasProgress */);

        if (actionCount > 0) {
            big.removeAllViews(com.android.internal.R.id.media_actions);
            for (int i = 0; i < actionCount; i++) {
                final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i));
                big.addView(com.android.internal.R.id.media_actions, button);
            }
        }
        styleText(big);
        hideRightIcon(big);
        applyTopPadding(big);
        big.setViewVisibility(android.R.id.progress, View.GONE);
        return big;
    }

这与其膨胀的布局(notification_template_material_big_media)相匹配,其中包含:

This matches with the layout it inflates (notification_template_material_big_media), which contains:

  • 媒体图像的布局.
  • 用于三个文本行的LinearLayout.
  • 添加了媒体操作的LinearLayout.
  • 一个ImageView来显示这两者之间的分隔线.

但没有别的.

文档页面中的关闭按钮似乎只是艺术家的表演(Google Play音乐也不包含).

It would appear that the close button in the documentation page is just an artist's rendition (Google Play Music doesn't include it, either).

这篇关于使用Android L Notification.MediaStyle添加终止播放按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆