我想使用新的 Android L MediaStyle 模板创建媒体播放通知。现在,我成功地完成了上一个、播放、暂停、下一个等操作(通过使用addAction()
,但我找不到像中那样添加“关闭”按钮的方法Android 通知文档 http://developer.android.com/design/patterns/notifications.html截屏:
有没有一种巧妙的方法来实现这一目标?我希望“关闭”按钮终止当前正在播放的播放,清除播放通知,并位于所附屏幕截图中。
UPDATE
当使用此通知样式的支持库版本时,即NotificationCompat.MediaStyle
,有setShowCancelButton() http://developer.android.com/reference/android/support/v7/app/NotificationCompat.MediaStyle.html#setShowCancelButton%28boolean%29 button.
这将添加关闭按钮,但仅限于 Lollipop 之前的版本,解决通知在作为前台服务的一部分显示后无法关闭的错误。
在 Lollipop 中,首选选项是能够关闭通知(当音频暂停时),而不是使用自定义按钮来执行此操作。
旧答案
从查看源代码来看Notification.MediaStyle
类目前似乎不支持 MediaStyle 通知样式中的此类按钮:
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
), 其中包含:
- 媒体图像的布局。
- 用于三个文本行的 LinearLayout。
- 添加媒体操作的 LinearLayout。
- 一个 ImageView 显示两者之间的分隔线。
但没有别的。
文档页面中的关闭按钮似乎只是艺术家的演绎(Google Play 音乐也不包含它)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)