android 广播接收器之通知栏页面跳转
来源:互联网 发布:公安网络安全保密协议 编辑:程序博客网 时间:2024/05/22 06:47
public class MyBroadcastReceiver extends BroadcastReceiver { private static int NOTIFY_ID = 1000; private static final String tag = "NotificationReceiver"; @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context, "Boot Complete", Toast.LENGTH_SHORT).show(); //推送消息 sendNotification(context,"12345"); } private void sendNotification(Context context, String mas){ //【1】获取Notification 管理器的参考 NotificationManager notifyMgr= (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); //【2】设置通知。PendingIntent表示延后触发,是在用户下来状态栏并点击通知时触发,触发时PendingIntent发送intent,本例为打开浏览器到指定页面。 Intent intent = new Intent(context,MainActivity.class); // intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // context.startActivity(intent ); // intent.setClassName(this,MainActivity.class); //intent.setComponent(new ComponentName(this, MainActivity.class)); //方法4 // 主要设置点击通知的时显示内容的类 PendingIntent pi = PendingIntent.getActivity(context, 0, intent, 0); Notification notification = new Notification.Builder(context) .setSmallIcon(R.drawable.logo_b) .setTicker("Hello") .setContentTitle("Title") .setContentText("Content text") .setContentIntent(pi) .build(); notification.flags |= Notification.FLAG_AUTO_CANCEL; //点击后删除,如果是FLAG_NO_CLEAR则不删除,FLAG_ONGOING_EVENT用于某事正在进行,例如电话,具体查看参考。 //【3】发送通知到通知管理器。第一个参数是这个通知的唯一标识,通过这个id可以在以后cancel通知,更新通知(发送一个具有相同id的新通知)。这个id在应用中应该是唯一的。 notifyMgr.notify(NOTIFY_ID, notification); }}
==========================================================================================================
Android广播机制指的是,在一个应用程序运行的时候可以自定义一个消息类型,让相应的接收器去处理这个消息或者是系统消息,比如来电话了、来短信了、手机没电了等等系统发送的消息。系统发送的消息也可以通过广播的方式通知给应用程序,这样子就避免了新开一个Thread去监听系统或其他应用发送过来的消息的状态。
Android广播的分类:
1、 普通广播:这种广播可以依次传递给各个处理器去处理
2、 有序广播:这种广播在处理器端的处理顺序是按照处理器的不同优先级来区分的,高优先级的处理器会优先截获这个消息,并且可以将这个消息删除
3、 粘性消息:粘性消息在发送后就一直存在于系统的消息容器里面,等待对应的处理器去处理,如果暂时没有处理器处理这个消息则一直在消息容器里面处于等待状态。
注意:普通广播和粘性消息不同被截获,而有序广播是可以被截获的
处理器的注册:
1、 在代码中用函数代码动态的方式注册。动态注册的处理器必须用代码动态的销毁,每次用来处理消息的就一个实例对象
2、 在配置文件里面静态注册,静态注册有个特点,那就是一旦注册就会一直存在于系统里面,无论应用是否关闭或开关机。(简直就是一个流氓软件病毒啊~)。静态注册每次有处理消息就由系统new一个处理器处理,并销毁
下面具体看看Android广播消息的发送、注册、处理过程:
① 自定义处理器类:
public class MyBroadcastReceiver4 extends BroadcastReceiver {
public MyBroadcastReceiver4() {
System.out.println("创建了一个由registerReceiver()注册的广播接收器");
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
System.out.println("MyBroadcastReceiver4收到了一个" + action + "消息");
if (isOrderedBroadcast()) {
System.out.println("这是一个有序广播,已经被拦截了。");
this.abortBroadcast();
} else {
System.out.println("这不是一个有序广播");
}
Bundle bundle = intent.getExtras();
if (bundle != null) {
System.out.println("该消息携带的数据如下:");
// 获得bundle的一个key的集合
Set set = bundle.keySet();
// 获得上述集合的迭代器
Iterator iterator = set.iterator();
// 用迭代器遍历集合
while (iterator.hasNext()) {
// 取得集合中的一个内容
String str = (String) iterator.next();
// 取得Bundle中的内容
System.out.println(str + "--->" + bundle.get(str));
}
} else {
System.out.println("该消息没有携带数据");
}
Toast toast = Toast.makeText(context, "MyBroadcastReceiver4收到了一个"
+ action + "消息", Toast.LENGTH_LONG);
toast.show();
//将这个消息截获(从消息容器移除)这样其他处理器就没法接收到这个消息
this.abortBroadcast();
}
}
而界面的转跳
Intent intent = new Intent(context,目标Activity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent );
提示: context 是在广播接收者的
public void onReceive(Context context, Intent intent) 方法中获取的
0 0
- android 广播接收器之通知栏页面跳转
- Android — 之广播接收器
- Android之BroadcastReceiver(Android中的广播接收器)
- Android学习之路-------广播接收器
- android四大组件之广播接收器BroadcastReceiver
- android四大组件之--->BroadcastReceiver广播接收器
- Android四大组件之广播接收器(一)
- android必备基础之广播接收器BroadcastReceiver
- 极光推送广播接收器 跳转~
- Android广播接收器监听多个广播及多个广播接收器监听同一个广播_广播之二
- BroadCastReceiver android 广播接收器
- android 广播与 接收器
- Android 广播接收器BroadcastReceiver
- Xamarin.Android广播接收器
- android中的广播接收器
- Android广播接收器
- android-----BroadcastReceiver广播接收器
- Android-BroadcastReceiver广播接收器
- Scala XML解析库
- hitTest、pointInside实例
- Google浏览器开发者工具:CSSViewer(一个Css查看器)
- HDU 1828 线段树之扫描线之周长并
- IOS 正则表达式
- android 广播接收器之通知栏页面跳转
- 你真的了解iOS代理设计模式吗?
- 增值税等普通发票扫描识别系统
- 主从同步设置的重要参数log_slave_updates
- 二分查找(改)
- LoaderManager和AsyncTaskLoader<D>详解
- AdpterView和Adapter接口以及实现的子类
- Ubuntu 15.10 编译Android源码环境搭建
- FZU2102Solve equation