android Notification与BroadcastReceiver结合使用
来源:互联网 发布:程序员的修炼之道 pdf 编辑:程序博客网 时间:2024/06/06 09:41
- NotificationUtils 类
public class NotificationUtils { private static final String TAG = "NotificationUtils"; static int i = 0; private static int id = 1; public static void setNotification(Context context, String content,String toUserId ) {// Intent intent = new Intent(Intent.ACTION_MAIN);// intent.addCategory(Intent.CATEGORY_LAUNCHER);// intent.setClass(MyApplication.getInstance(), MainActivity.class);// RxBus.getDefault().post(Constants.NOTIFICATION); MilierLog.i(TAG,"setNotification***"); Intent intent = new Intent(MyApplication.getInstance(), NotificationClickReceiver.class); intent.setAction("notification_clicked"); intent.putExtra(NotificationClickReceiver.TYPE, id); MilierLog.i(TAG,"通知里toUserId is :"+toUserId); Constants.TO_USER_ID = toUserId; intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);// PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); PendingIntent pendingIntent = PendingIntent.getBroadcast(MyApplication.getInstance(), id++, intent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE); Notification.Builder builder = new Notification.Builder(context);// if (content.equals(context.getString(R.string.newMessage))) {// builder.setContent(new RemoteViews(context.getPackageName(), R.layout.layout_notification));// } else {// builder.setContent(new RemoteViews(context.getPackageName(), R.layout.layout_notification));// } builder.setContentIntent(pendingIntent)//设置通知栏点击意图 .setContentTitle(content) .setTicker(content) .setWhen(System.currentTimeMillis())//通知产生的时间,会在通知信息里显示,一般是系统获取到的时间 .setAutoCancel(true)//设置这个标志当用户单击面板就可以让通知将自动取消 .setOngoing(false)//ture,设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接) .setDefaults(Notification.DEFAULT_ALL)//向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置,使用defaults属性,可以组合 .setSmallIcon(R.mipmap.ic_launcher);//设置通知小ICON Notification notification = builder.getNotification(); notificationManager.notify(i++, notification); }}
重要的两点:
PendingIntent.getBroadcast(MyApplication.getInstance(), id++, intent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent.getBroadcast的第四个参数设置于PendingIntent.FLAG_UPDATE_CURRENT
第二个参数区分注册的Intent
2.NotificationClickReceiver 类
public class NotificationClickReceiver extends BroadcastReceiver { private static final String TAG = "NotificationClickReceiver"; public static final String TYPE = "type"; @Override public void onReceive(Context context, Intent intent) { //todo 跳转之前要处理的逻辑 String action = intent.getAction(); int type = intent.getIntExtra(TYPE, -1); if (type != -1) { NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.cancel(type); } MilierLog.i(TAG,"action is :"+action); if (action.equals("notification_clicked")) { //处理点击事件 MilierLog.i(TAG,"NotificationClickReceiver***");// Intent newIntent = new Intent(context, MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);// context.startActivity(newIntent); RxBus.getDefault().post(Constants.NOTIFICATION); } if (action.equals("notification_cancelled")) { //处理滑动清除和点击删除事件 } }}
- AndroidManifest.xml中注册监听
<receiver android:name=".broadcast.NotificationClickReceiver"> <intent-filter> <action android:name="notification_cancelled"/> <action android:name="notification_clicked"/> </intent-filter> </receiver>
阅读全文
0 0
- android Notification与BroadcastReceiver结合使用
- BroadcastReceiver与Notification的结合使用实现短信完全自主接收
- BroadcastReceiver与ListView结合使用
- 【Android】使用广播BroadcastReceiver拦截通知Notification
- 使用BroadcastReceiver处理Notification
- 77 Android BroadcastReceiver 和 Notification
- Android 四大组件之(3)BroadcastReceiver广播事件处理以及与Notification的配合使用详解
- Service与Notification的结合使用实现文件下载
- android开发之Notification结合PendingIntent使用小结
- android notification和peddingintent结合使用遇到的问题
- Android Notification的学习与使用
- 黑马程序员------------------Android SharedPreferences与BroadcastReceiver简单结合(手机号码自动加拨)
- Android LocalBroadcastManager与BroadcastReceiver
- [android]Broadcast与BroadcastReceiver
- [Android] BroadCastReceiver的使用
- Android中BroadCastReceiver使用
- Android中BroadCastReceiver使用
- Android BroadcastReceiver 使用 SQLite
- Spring3.1.0实现原理分析(十七).MVC请求处理方法参数解析器和返回值解析器
- 基本算法之选择排序
- object-c 入门基础篇
- JAVA操作HDFS案例
- C. Dasha and Password-C. Dasha and Password-暴力|dp思想
- android Notification与BroadcastReceiver结合使用
- 运筹学之排队论
- Centos 6.x docker的安装配置
- TensorFlow数据归一化
- 详解数据库存储引擎
- Mac 环境下安装OpenCV
- PhpStorm中输入框无法跟随问题的解决方案
- 为什么要清除浮动?
- javascript正则表达式笔记