我们以音乐播放器为例子做一个通知到 通知栏(并且我们要求点击按钮切换通知栏按钮状态并做切歌的操作)
我们先写一个广播

public class XMPlayerReceiver extends BroadcastReceiver {public static final String PLAY_PRE = "play_pre";public static final String PLAY_NEXT = "play_next";public static final String PLAY_PAUSE = "play_pause";public static final String PLAY_PLAY = "play_play";@Overridepublic void onReceive(Context context, Intent intent) {if (intent.getAction().equals(PLAY_NEXT)){//PLAY_NEXTLog.e("XMPlayerReceiver", "通知栏点击了下一首");}if (intent.getAction().equals(PLAY_PRE)) {;Log.e("XMPlayerReceiver", "通知栏点击了上一首");}if (intent.getAction().equals(PLAY_PAUSE)) {Log.e("XMPlayerReceiver", "通知栏点击了暂停");}if (intent.getAction().equals(PLAY_PLAY)) {Log.e("XMPlayerReceiver", "通知栏点击了开始");}}
}

AndroidManifest.xml中注册上我们写的广播

        <receiverandroid:name=".app.audioheler.ability.XMPlayerReceiver"android:exported="true"android:process=":player"><intent-filter><action android:name="play_pre" /><action android:name="play_next" /><action android:name="play_pause" /><action android:name="play_play" /></intent-filter></receiver>

准备好广播之后我们发一个推送到通知栏。

先自定义一个RemoteViews ,xml自己写

private RemoteViews initNotifyView(Bitmap bitmap) {String packageName = context.getPackageName();RemoteViews remoteView = new RemoteViews(packageName, R.layout.你的xml);remoteView.setImageViewBitmap(R.id.xml中放图的imageview id, Bitmap位图);remoteView.setTextViewText(R.id.xml中标题的id, "标题内容");remoteView.setTextViewText(R.id.xml中小标题的id, "小标题内容");Intent prv = new Intent(context,XMPlayerReceiver.class);//播放上一首prv.setAction(PLAY_PRE);PendingIntent intent_prev = PendingIntent.getBroadcast(context, 1, prv,PendingIntent.FLAG_UPDATE_CURRENT);remoteView.setOnClickPendingIntent(R.id.上一首按钮的id, intent_prev);Intent next = new Intent(context,XMPlayerReceiver.class);//播放下一首next.setAction(PLAY_NEXT);PendingIntent intent_next = PendingIntent.getBroadcast(context, 2, next,PendingIntent.FLAG_UPDATE_CURRENT);remoteView.setOnClickPendingIntent(R.id.下一首按钮的id, intent_next);Intent startpause = new Intent(context,XMPlayerReceiver.class);//暂停startpause.setAction(PLAY_PAUSE);PendingIntent intent_pause = PendingIntent.getBroadcast(context, 3, startpause,PendingIntent.FLAG_UPDATE_CURRENT);remoteView.setOnClickPendingIntent(R.id.暂停按钮的id, intent_pause);Intent startplay = new Intent(context,XMPlayerReceiver.class);//播放startplay.setAction(PLAY_PLAY);PendingIntent intent_play = PendingIntent.getBroadcast(context, 4, startplay,PendingIntent.FLAG_UPDATE_CURRENT);remoteView.setOnClickPendingIntent(R.id.播放按钮的id, intent_play);return remoteView;}

现在把自定义的样式发到通知栏上

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);String channelId = "notification";if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//适配一下高版本NotificationChannel channel = new NotificationChannel(channelId,"listen",NotificationManager.IMPORTANCE_DEFAULT);channel.enableLights(false); //是否在桌面icon展示小红点channel.setLightColor(Color.RED); //小红点颜色channel.setSound(null, null);//关了通知默认提示音channel.setShowBadge(false); //是否在久按桌面图标时显示此渠道的通知notificationManager.createNotificationChannel(channel);}NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId).setSmallIcon(R.mipmap.ic_launcher)//这玩意在通知栏上显示一个logo.setCategory(CATEGORY_MESSAGE).setDefaults(DEFAULT_ALL).setOngoing(true);//点击通知栏跳转的activityIntent intent = new Intent(context, NotifyActivity.class);PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);builder.setAutoCancel(false);//点击不让消失builder.setSound(null);//关了通知默认提示音builder.setPriority(PRIORITY_MAX);//咱们通知很重要builder.setVibrate(null);//关了车震builder.setContentIntent(pendingIntent);//整个点击跳转activity安排上builder.setOnlyAlertOnce(false);RemoteViews remoteViews = initNotifyView(bitmap);builder.setContent(remoteViews);//把自定义view放上builder.setCustomBigContentView(remoteViews);//把自定义view放上Notification notification = builder.build();notification.flags |= FLAG_ONGOING_EVENT;notification.flags |= Notification.FLAG_NO_CLEAR;//不让手动清除 通知栏常驻notification.sound = null;//关了通知默认提示音notificationManager.notify(notifyId, notification);

需要的时候调用上段代码将通知发到通知栏上  在BroadcastReceiver 中接收点击事件。若更新通知栏的样式,重新创建RemoteViews再发一遍通知好了  notifyId使用同一个。

当我们不需要这个通知栏播放器时  直接关闭通知

notificationManager.cancel(notifyId);

这俩biang玩意别跟别的app重了  带上自己包名

如有不重发通知更新通知栏的方法请告诉我

android 发音乐通知到通知栏相关推荐

  1. android点击通知后消失,通知栏点击后消失解决方法

    通知栏点击后消失 怎么使通知栏点击后消失啊?以下为我的代码,能实现将信息放入通知栏,但是点击后不消除... private void showNotification(String temp) { / ...

  2. Android中使用Notification在通知栏中显示通知

    场景 App在接收到后台推送的消息后,需要在系统通知栏中显示通知消息,并且点击通知消息跳转到新的页面,并将消息内容传递过去. 效果如下 注: 博客: https://blog.csdn.net/bad ...

  3. android 禁用通知栏_如何在Android上禁用通知

    android 禁用通知栏 Notifications are great, and Android's notification system is arguably the best out th ...

  4. Android代码删除通知,Android:从通知中删除通知b

    Android:从通知中删除通知b 我已经创建了一个应用程序,并且我设法在android通知栏中添加通知. 现在我需要示例如何从事件通知栏中删除该通知? 11个解决方案 197 votes 你可以尝试 ...

  5. 【android】音乐播放器之设计思路

    学习Android有一个多月,看完了<第一行代码>以及mars老师的第一期视频通过音乐播放器小项目加深对知识点的理解.从本文开始,将详细的介绍简单仿多米音乐播放器的实现,以及网络解析数据获 ...

  6. 【android】音乐播放器之service服务设计

    学习Android有一个多月,看完了<第一行代码>以及mars老师的第一期视频通过音乐播放器小项目加深对知识点的理解.从本文开始,将详细的介绍简单仿多米音乐播放器的实现,以及网络解析数据获 ...

  7. Android开源音乐播放器之播放器基本功能

    系列文章 Android开源在线音乐播放器--波尼音乐 Android开源音乐播放器之播放器基本功能 Android开源音乐播放器之高仿云音乐黑胶唱片 Android开源音乐播放器之自动滚动歌词 An ...

  8. 【Android】状态栏通知Notification、NotificationManager详解

    在Android系统中,发一个状态栏通知还是很方便的.下面我们就来看一下,怎么发送状态栏通知,状态栏通知又有哪些参数可以设置? 首先,发送一个状态栏通知必须用到两个类: NotificationMan ...

  9. Android点击通知进入详情,Android 点击通知进入正在运行的程序

    好久没写文章,今天遇到一个新需求,app正在通话过程切换到后台,点击通知栏的时候 回到通话界面. 直接上代码吧首先是实现通知的代码 NotificationCompat.Builder notific ...

最新文章

  1. SAP HUM 嵌套HU初探 II
  2. Microsoft Azure部署MYSQL-MMM(3)配置MYSQL-MMM
  3. Qt+Phonon的另一种选择
  4. DataSnap如何监控Tcp/IP客户端的连接情况
  5. 深度探索C++ 对象模型(5)-Initialization list(2)
  6. tensorflow学习(5.实际图片的读取以及lenet-5的搭建)
  7. 学习总结——Selenium元素定位
  8. android merge的作用,Android学习手记-merge
  9. LeetCode 1000. 合并石头的最低成本(区间DP)
  10. 苹果推送通知服务(APNs)编程(转)详细步骤
  11. MFC简单的登入界面设计
  12. 如何用“云崽(Yunzai)”搭建一个原神群机器人(转载)
  13. matlab能不能查焓湿图,用MATLAB绘制南昌地区室外气象焓湿频图程序.docx
  14. FAT32 文件系统详解
  15. 分析google关键词de工具
  16. VS连接数据库运行后显示对象名无效
  17. 详解163、CN2-GT和CN2-GIA的区别
  18. C语言实现简单小游戏---扫雷
  19. css vw单位_使用CSS vw单位创建完美的响应形状
  20. 数学建模的影响因素分析方法

热门文章

  1. Git git update-index --assume-unchanged
  2. Sequential regulatory activity prediction across chromosomes with convolutional neural networks
  3. SAP中总账科目行项目不显示的处理方法
  4. matlab中opc没有注册类,电脑中出现没有注册类别的错误提示的多种解决方法
  5. WIFI 认证加密模式介绍 理解无线安全
  6. 马王堆汉墓帛书‧老子乙本——道经
  7. 虚拟服务器lan网段地址,lan侧服务器ip地址
  8. word 2019 深黑色背景如何改成白色(已解决)
  9. 领英中国总裁陆坚:顺应本土化发展,领英进入3.0阶段 。我们该如何正确使用领英
  10. 前程似锦 如鱼得水 藏头诗