这是我用来创建媒体控件通知的代码:
MediaDescriptionCompat description = Metadata.getDescription();String artistname = Metadata.getString(MediaMetadataCompat.MetaDATA_KEY_ARTIST);String albumname = Metadata.getString(MediaMetadataCompat.MetaDATA_KEY_ALBUM);Bitmap largeIcon = BitmapFactory.decodeResource(playbackService.getResources(),R.drawable.vector_global_defaultsong);NotificationCompat.Builder notificationBuilder = new NotificationCompat .Builder(playbackService,NotificationHelper.CHANNEL_MEDIA_CONTRolS) .setcolor(ContextCompat.getcolor(playbackService,R.color.colorAccent)) .setSmallicon(R.drawable.logo_light_filled) .setContentTitle(description.getTitle()) .setContentText(playbackService.getString(R.string.song_List_subTitle_format,artistname,albumname)) .setContentIntent(createContentIntent()) .setDeleteIntent(MediabuttonReceiver.buildMediabuttonPendingIntent(playbackService,PlaybackStateCompat.ACTION_Stop)) .setVisibility(NotificationCompat.VISIBIliTY_PUBliC) .setongoing(playbackState.getState() == PlaybackStateCompat.STATE_PLAYING) .setLargeIcon(largeIcon);notificationBuilder.setStyle(new androID.support.v4.media.app.NotificationCompat.MediaStyle() // show prevIoUs,play/pause,and next in compact vIEw .setShowActionsInCompactVIEw(addActions(notificationBuilder)) .setMediaSession(sessionToken));showNotification(notificationBuilder.build());
. . .
private voID showNotification(final Notification notification) { if (!started) { mediaController.registerCallback(mediaControllerCallback); playbackService.startForeground(NOTIFICATION_ID,notification); started = true; } else { notificationmanager.notify(NOTIFICATION_ID,notification); } if (playbackState.getState() == PlaybackStateCompat.STATE_PAUSED) { playbackService.stopForeground(false); }}
这是我用来创建通知渠道的代码:
public static final String CHANNEL_MEDIA_CONTRolS = "media_controls";public static voID createNotificationChannels(Context context) { if (androID.os.Build.VERSION.SDK_INT >= 26) { NotificationChannel mediaControlsChannel = new NotificationChannel(CHANNEL_MEDIA_CONTRolS,context.getString(R.string.notification_channel_media_controls),notificationmanager.importANCE_HIGH); mediaControlsChannel.setShowBadge(false); getnotificationmanager(context).createNotificationChannel(mediaControlsChannel); }}
更新:在通知通道上将showBadge设置为false似乎不执行任何 *** 作.当显示媒体控件通知时,我仍会在应用程序图标上收到徽章.所以看起来我设置的通知通道属性没有被应用.
解决方法 根据 Migrating MediaStyle notifications to Android O,您应该使用importANCE_LOW,它不包含任何声音 – importANCE_HIGH频道有与之相关的声音.AndroID已经在托盘中重新排序了更高的MediaStyle通知,因此不需要使用更高的重要性,就像在以前的AndroID版本中一样.
注意:更改重要性后,您需要清除应用程序数据或重新安装应用程序,以使此更改在通知渠道中生效.
总结以上是内存溢出为你收集整理的媒体控制通知在Android O上发出警报全部内容,希望文章能够帮你解决媒体控制通知在Android O上发出警报所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)