有序广播修改添加bundle所带数据
来源:互联网 发布:浙江大学海洋学院,知乎 编辑:程序博客网 时间:2024/06/06 04:31
突然想到有序广播测试时,自己忘了对其所带参数进行修改和添加测试了,在这里补上吧!
代码如下:
package com.btzh.mybroatcasttest;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.widget.Toast;public class MyOrderReceiver extends BroadcastReceiver { public MyOrderReceiver() { } @Override public void onReceive(Context context, Intent intent) { if ("order_Broatcast".equals(intent.getAction())){ Bundle bundle = new Bundle(); bundle.putString("AAA", "下一个广播接收者"); setResultExtras(bundle); String name = intent.getStringExtra("name"); System.out.println("-----"+"我是有序广播AAA"+name); Toast.makeText(context,"我是AAA:"+name,Toast.LENGTH_SHORT).show(); //加上这句代码,阻断广播的发送到下一个,注释掉,可以发送到下一个// abortBroadcast(); } }}
第二个广播接收器
package com.btzh.mybroatcasttest;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.widget.Toast;public class MyOrderReceivertwo extends BroadcastReceiver { public MyOrderReceivertwo() { } @Override public void onReceive(Context context, Intent intent) { if ("order_Broatcast".equals(intent.getAction())){ String name = intent.getStringExtra("name"); Bundle aaaa = getResultExtras(true); String aaa = aaaa.getString("AAA"); Bundle twobundle = new Bundle(); twobundle.putString("AAA","我被修改了!"); setResultExtras(twobundle); System.out.println("-----"+"我是有序广播BBB"+name); Toast.makeText(context,"我是BBB:"+name+aaa,Toast.LENGTH_SHORT).show(); } }}
第三个广播接受器
package com.btzh.mybroatcasttest;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.widget.Toast;public class MyReceiverThree extends BroadcastReceiver { public MyReceiverThree() { } @Override public void onReceive(Context context, Intent intent) { if ("order_Broatcast".equals(intent.getAction())){ String name = intent.getStringExtra("name"); Bundle aaaa = getResultExtras(true); String aaa = aaaa.getString("AAA"); System.out.println("-----"+"我是有序广播BBB"+name+aaa); Toast.makeText(context,"我是BBB:"+name+aaa,Toast.LENGTH_SHORT).show(); } }}
清单文件代码:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.btzh.mybroatcasttest"> <!-- 接受另一个APP发送广播所需要的权限 --> <!-- <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> --> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".MyReceiverOne" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="not_order_broadcast" /> </intent-filter> </receiver> <!-- exported:这个属性用于指示该服务是否能够被其他应用程序组件调用或跟它交互。 如果设置为true,则能够被调用或交互,否则不能。设置为false时, 只有同一个应用程序的组件或带有相同用户ID的应用程序才能启动或绑定该服务 --> <receiver android:name=".MyOrderReceiver" android:enabled="true" android:exported="true"> <intent-filter android:priority="1000"> <action android:name="order_Broatcast" /> </intent-filter> </receiver> <receiver android:name=".MyOrderReceivertwo" android:enabled="true" android:exported="true"> <intent-filter android:priority="900"> <!-- priority属性值最大值为1000,数值越大优先级越高 action 是我们为了区分不同广播取得任意别名 --> <action android:name="order_Broatcast" /> </intent-filter> </receiver> <receiver android:name=".MyReceiverThree" android:enabled="true" android:exported="true"> <intent-filter android:priority="800"> <action android:name="order_Broatcast"/> </intent-filter> </receiver> </application></manifest>
运行到手机上后,我们可以通过打印信息观察广播传递过程中的参数添加于改变!
1 0
- 有序广播修改添加bundle所带数据
- Broadcast详解(有序广播,无序广播)最终广播,Bundle传递参数
- 在有序广播中添加自定义权限
- 添加Bundle的元数据
- 17_Android中Broadcast详解(有序广播,无序广播)最终广播,Bundle传递参数,传递参数的时候指定权限
- 有序广播
- 有序广播
- 有序广播
- 有序广播
- 有序广播
- 有序广播
- 有序广播
- 有序广播
- 有序广播
- 有序广播
- 有序广播
- 有序广播
- 有序广播
- leetcode题目整理
- java同步器之CountDownLatch
- 网页数据采集-webcollector学习与实践
- PropertyUtils.getProperty和PropertyUtils.setProperty的用法详解
- 总结适配IOS10需要注意的问题
- 有序广播修改添加bundle所带数据
- (转)Android中的Pending.getActivity()参数详解
- win10安装xgboost到python库
- 解析xml获取所需文本、属性信息
- D3D SetSamplerState
- 为什么我的燃尽图没更新?
- iOS10适配总结
- Android实现横向滑动的GridView
- struts2 <s:iterator> status属性