一.普通广播(无序广播)
1.首先要了解一下无序无播的生命周期,API中写到广播生产周期,提供一个有效期间表示调用onReceive有效的,一但方法执行完毕对象finsh掉了, 可以在onReceive方法中做任何异步的操作。
2.使用自定义无序广播要有发送者设置一个意图setAction("xxx");广播接收者需在AndroidManifest.xml中过滤意图才能接收到广播。
代码如下:
广播的发送
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) this.findViewById(R.id.button1); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub // 发送广播,一个意图(普通无序的) Intent intent = new Intent(MainActivity.this, MyBrocastReceiver.class); intent.setAction("abc"); intent.putExtra("name", "kaitao"); sendBroadcast(intent); } }); }
广播的接收可以在onReceive中做任意的异步操作
//广播接收者 public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub String name = intent.getStringExtra("name"); Toast.makeText(context, name, 1).show(); }
广播的接收也可以做一个通知
public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub manager = (NotificationManager) context .getSystemService(Context.NOTIFICATION_SERVICE); String name = intent.getStringExtra("name"); // 做一个通知,不能在onReceive里面用AerltDialog NotificationCompat.Builder builder = new NotificationCompat.Builder(context); builder.setContentTitle("没有网了"); builder.setContentText("wifi掉了"); builder.setTicker("广播来了"); builder.setSmallIcon(R.drawable.ic_launcher); manager.notify(1001, builder.build()); }
AndroidManifest.xml设置
这里<receiver android:name=".MyBrocastReceiver2">是指定哪个类接收广播。
而 <action android:name="abc"/>则是过滤条件, intent.setAction("abc");。
<!-- 注册广播接收者 --> <receiver android:name=".MyBrocastReceiver"> <intent-filter> <action android:name="abc"/> </intent-filter> </receiver> <receiver android:name=".MyBrocastReceiver2"> <intent-filter> <action android:name="abc"/> </intent-filter> </receiver> <receiver android:name=".MyBrocastReceiver3"> <intent-filter> <action android:name="abc"/> </intent-filter> </receiver>
二、有序广播
使用有序广关健处在于定义接收者的优先权,以下代码中有三个类接收广播,
<intent-filter android:priority="1000">是定义接收的先后权限,priority值越大其优先权越大。
<receiver android:name=".One"> <intent-filter android:priority="900"> <action android:name="abc"/> </intent-filter> </receiver> <receiver android:name=".Two"> <intent-filter android:priority="1000"> <action android:name="abc"/> </intent-filter> </receiver> <receiver android:name=".There"> <intent-filter android:priority="800"> <action android:name="abc"/> </intent-filter> </receiver>
如果在接收者类中onReceive方法中,使用abortBroadcast();即终止广播,后面的广播不会在继续。
相关推荐
传智播客_Andorid_20天精通Android开发视频_第11天视频_视频_09_发送有序广播&有序广播和无序广播的区别.avi
传智播客_Andorid教程_20天从零到精通Android开发视频_第11天 09_有序广播和无序广播的区别
详细介绍广播的用法,代码有注释,简明易懂,实现广播的两种形式,有序广播和无序广播;详细介绍广播的用法,代码有注释,简明易懂,实现广播的两种形式,有序广播和无序广播;详细介绍广播的用法,代码有注释,简明...
(1)无序广播是完全异步执行,发送广播时所有监听这个广播的广播接收者都会收到此消息,但接收的顺序不确定。 (2)有序广播是按照接收者的优先级接收,只有一个广播接收者能接收信息,在此广播接收者中逻辑执行...
android中广播接收者,有序广播和无序广播 sendOrderBroadcast sendBroadcast
介绍了有序广播,无序广播,本地广播,开机广播,应用安装缷载广播,SD卡装挂载广播,锁屏开屏广播,自定义广播等
本案例的源码展示了android中基本的广播使用,其中包括有序广播、无序广播的使用,包括广播接收者的静态注册和动态注册,包括如何终止广播等案例。
这里介绍如何创建广播、如何发送一个无序广播和有序广播、以及监听短信以及监听呼出电话(当我们发短信和打电话,系统就会发一个广播,我们可以拦截此广播进行监听短信以及监听呼出电话)。 定义广播接收者 1.定义类...
主要讲解下有序广播和无序广播的区别,以及动态注册和静态注册的使用,有代码注释!
BroadCast 有序广播, 无序广播,动态广播,静态广播
自己写的broadcast(normal broadcast,order broadcast)的demo,包括静态广播,动态广播,有序广播和无序广播。可供参考。。。。
使用人群:BroadcastReceiver初学者。...里面涉及BroadcastReceiver的静态注册(无序广播)、动态注册(无序广播)、有序广播。 项目对应:https://shuaici.blog.csdn.net/article/details/118337894
广播可以分为有序广播和无序广播两种。 * 有序广播:优先级高的广播接收器先接收广播。 * 无序广播:所有广播接收器同时接收广播。 BroadcastReceiver 有两种注册方式,即静态注册和动态注册。静态注册需要在 ...
实现了广播的各种使用,包括:有序广播、无序广播、本地广播;实现了广播的两种注册方式:动态注册、静态注册;实现了不同类型广播的接收:系统广播、自定义广播;除此之外,进行了广播优先级的设置,并对相应的广播...
详解Android开发初稿序言全本目录 序言 目录中打勾的表示已汇总,暂时在不断学习汇总。 全本目录 ... 无序广播详解 有序广播详解 Service(服务)概要介绍 Android 服务(Service) IntentServic
大力整合城市管理中与旅游密切相关的基础信息系统,对于信息化程度落后的领域加快信息化,消灭信息孤岛,保障各个环节中信息的无障碍流动,通过信息的充分流动,将会为景区和主管部门在掌握重要信息、平衡旅游资源、...
广播有序调度 广播无序调度 自定义策略 一体化 谷歌Guice Mycila 发布订阅 Mycila Event 是一个新的强大的事件框架,用于内存中事件管理。 它有许多类似于EventBus的功能,但编写得更好,并使用 Java 并发功能为您...
一维数据:列表(有序)和集合类型(无序) 二维及高维数据:列表 高维数据:字典类型及数据表达格式(JSON,XML,YAML)*.numpy功能: 1.提供了强大的N维数组对象ndarray 2.广播功能函数 3.整合c/c++/fortran代码的工具 4...