自定义有序广播并接受信息
来源:互联网 发布:佐治亚州知乎 编辑:程序博客网 时间:2024/05/18 03:42
在上篇博文中点击打开链接,我们简单介绍了Android中无序广播的实现,那么在本篇博文中,我们将重点介绍的是有序广播的实现。
所谓的有序广播就是:应用按照一定的顺序接受广播,该顺序是根据权限值定义的,权限值越大则最先获取广播,权限值小的则后获取广播。
有序广播可以拦截,如果发生拦截,则之后无法继续传递广播。
我们发送有序广播使用的是:sendBroadcast(intent);
而我们发送有序广播使用的是:sendOrderedBroadcast(intent, null);
其中我们还需在过滤其中设置接受信息的优先级,使用的方法是: android:priority="450"设置接受有序广播的优先级 ,数值越大,优先级越高,则最先收到广播
好了,说了这么多我们看一下具体的代码实现吧。
一、首先是在本应用中接受广播的信息。
首先是在MainActivity定义广播发送者:同样我们需要设置发送广播的Intent的Action
package com.example.text04;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.content.IntentFilter;import android.view.Menu;import android.view.View;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 这里同样可以静态注册广播并设置优先级// IntentFilter filter = new IntentFilter();// filter.addAction("MyInfo02");// filter.setPriority(400);// registerReceiver(new MyReceiver02(), filter);}public void send(View view) {// 创建Intent对象Intent intent = new Intent();intent.putExtra("info", "最近过的好吗?");intent.putExtra("num", 100);intent.setAction("MyInfo02");// 发送广播sendOrderedBroadcast(intent, null);}}然后是在本应用中创建BroadCastReceiver的两个子类实现广播的接受:同样是先判断该Intent的Action是否和我们发送广播时设置的Action一致。
第一个子类:
package com.example.text04;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;public class MyReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {if (intent.getAction().equals("MyInfo02")) {String info = intent.getStringExtra("info");int num = intent.getIntExtra("num", 88);//Log输出Log.i("main", "01收到的信息:" + info + " 数值是:" + num);}}}
第二个子类:
package com.example.text04;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;public class MyReceiver02 extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {if (intent.getAction().equals("MyInfo02")) {String info = intent.getStringExtra("info");int num = intent.getIntExtra("num", 88);// Log输出Log.i("main", "02收到的信息:" + info + " 数值是:" + num);}}}然后是在清单文件中加上Receiver的过滤器:这里我们使用android:proiority设置两个接受器的权限值,越大的越先接受到信息。
<receiver android:name="com.example.text04.MyReceiver" > <intent-filter android:priority="500" > <action android:name="MyInfo02" /> </intent-filter> </receiver> <receiver android:name="com.example.text04.MyReceiver02" > <intent-filter android:priority="400" > <action android:name="MyInfo02" /> </intent-filter> </receiver>二、以上就是在本应用中接受自定义的有序广播,接下来我们看一下在其他应用中如何接受广播的信息。
同样我们创建一个新的Android工程,然后新建一个BroadCastReceiver的子类进行信息的接受:同样是判断Action
package com.example.text05;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;public class MyReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {if (intent.getAction().equals("MyInfo02")) {String info = intent.getStringExtra("info");int num = intent.getIntExtra("num", 88);// Log输出Log.i("main", "other收到的信息:" + info + " 数值是:" + num);}}}然后是在清单文件中设置过滤器并配置该接收器的优先级:这里同样进行优先级的设定
<!-- android:priority="450"是设置接受有序广播的优先级 数值越大,优先级越高,则最先收到广播 --> <receiver android:name="com.example.text05.MyReceiver" > <intent-filter android:priority="450" > <action android:name="MyInfo02" /> </intent-filter> </receiver>好了,定义了优先级后,我们接受广播就有了一定的顺序,这就是有序广播的所有内容。我们唯一要注意的一点就是它和无序广播的两个区别,
就是发送广播的方法不同,还有就是有序广播需要定义优先级以确定接收器接受广播的顺序。
0 0
- 自定义有序广播并接受信息
- 自定义无序广播并接受信息
- 自定义 发送广播,并接受广播
- 24 接受自定义的广播
- Android自定义发送有序广播
- android之自定义有序广播
- 发送自定义广播 之 有序广播
- android自定义广播_有序广播
- 标准广播,有序广播,本地广播和自定义广播
- 在有序广播中添加自定义权限
- Android系列之自定义有序广播
- Android BroadcastReceiver之自定义无序、有序广播
- android,自定义广播,最终广播接收者,防止拦截广播,有序广播,无序广播
- android之旅16 自定义广播与无序有序广播
- 有序广播
- 有序广播
- 有序广播
- 有序广播
- codeforces #325
- 结构化程序设计和面向对象程序设计的特点及优缺点
- 数据结构基础3_带头结点的单链表
- 黑马程序员———I/O流
- POJ 1384 Piggy-Bank (完全背包)
- 自定义有序广播并接受信息
- [每日一答] [20151013] MySQL中的GROUP_CONCAT()函数详解
- 关于百度地图只显示中间部分,显示不全的原因
- C#生成Code39条形码而非条形码字体的方法
- WebMagic 爬虫框架学习
- 判断NSString中是否包含某个字符
- IOS开发者常用的12个Xcode插件
- MYSQL的常用命令和增删改查语句和数据类型
- 代理事件获取点击对应Cell