有序广播修改添加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
原创粉丝点击