java – 如何在API 26中添加音乐播放器通知
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 如何在API 26中添加音乐播放器通知,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1909字,纯文字阅读大概需要3分钟。
内容图文
![java – 如何在API 26中添加音乐播放器通知](/upload/InfoBanner/zyjiaocheng/718/36e55acbccfd4ac494ff4d7b0ce86646.jpg)
我已设法显示通知,但问题是这没有按预期工作.问题是 –
1.我不知道如何将通知保持在最顶层
2.当我滑动通知时,它会显示贪睡图标,但不会显示在任何其他音乐应用中.
这是我的代码
`if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
RemoteViews expandedView = new RemoteViews(getContext().getPackageName(), R.layout.custom_push);
CharSequence name = "Music Player";
String description = "Play music in background";
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel channel = new NotificationChannel("Music Player", name, importance);
channel.setDescription(description);
channel.setShowBadge(false);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationCompat.Builder nc = new NotificationCompat.Builder(getContext(),"Music Player");
Intent notifyIntent = new Intent(getContext(), now_playing.class);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(getContext(),0,notifyIntent,PendingIntent.FLAG_UPDATE_CURRENT);
nc.setContentIntent(pendingIntent);
nc.setCustomBigContentView(expandedView);
nc.setSmallIcon(R.drawable.notification_icon);
nc.setAutoCancel(false);
nc.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
nc.setOngoing(true);
NotificationManager notificationManager = getActivity().getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
notificationManager.notify(1, nc.build());
}`
另外我注意到其他音乐播放器在设置中没有任何通知频道.如果有人告诉我如何正确实施通知,那将会很棒.
谢谢
解决方法:
根据https://developer.android.com/training/notify-user/channels,
1)只能使用NotificationManager .IMPORTANCE_HIGH设置通知的优先级
2)有必要知道其他音乐应用频道CHANNEL_ID的标识符,也许这篇文章可以帮助你:https://medium.com/google-developers/migrating-mediastyle-notifications-to-support-android-o-29c7edeca9b7
内容总结
以上是互联网集市为您收集整理的java – 如何在API 26中添加音乐播放器通知全部内容,希望文章能够帮你解决java – 如何在API 26中添加音乐播放器通知所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。