Broadcast发送有序广播

这里一般和有序的代码全部罗列出来 但本章侧重于关注有序广播的发送与接收内容

发送端main-xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="sendNormalBC"
        android:text="发送一般广播" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="sendOrderBC"
        android:text="发送有序广播" />
</LinearLayout>

发送端Activity代码

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void sendNormalBC(View v) {
        Log.i("godv", "发送一般广播");
        //隐式意图 跟接收器的action对应
        Intent intent = new Intent("godv");
        intent.putExtra("MESSAGE", "godv");
        intent.setPackage("com.example.broadcastr");
//        intent.setClassName("com.example.broadcastr",);
//        "com.example.broadcastr.MyR1eceive"
        sendBroadcast(intent);
        Toast.makeText(this, "发送一般广播", Toast.LENGTH_SHORT).show();
    }

    public void sendOrderBC(View v) {
        Log.i("godv", "发送有序广播");
        //隐式意图 跟接收器的action对应
        Intent intent = new Intent("godv2");
        intent.putExtra("MESSAGE", "godv");
        intent.setPackage("com.example.broadcastr");
//        intent.setClassName("com.example.broadcastr",);
//        "com.example.broadcastr.MyR1eceive"
        //权限先设置为null
        sendOrderedBroadcast(intent, null);
        Toast.makeText(this, "发送有序广播", Toast.LENGTH_SHORT).show();
    }

}

接收端新建两个类继承BroadcastReceiver因为是同样的代码所以这里只展示一个

public class MyR3eceive extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String message = intent.getStringExtra("MESSAGE");
        Log.i("godv", "MyR3eceive--接收广播" + message);
    }

    public MyR3eceive() {
        Log.i("godv", "MyR3eceive--创建");
    }
}

注册这两个接收端(静态)

<receiver
    android:name=".MyR3eceive"
    android:enabled="true"
    android:exported="true">
    <intent-filter android:priority="1000">
        <action android:name="godv2" />
    </intent-filter>
</receiver>
<receiver
    android:name=".MyR4eceive"
    android:enabled="true"
    android:exported="true">
    <!--优先级android:priority int类型-->
    <intent-filter android:priority="2147483647">
        <action android:name="godv2" />
    </intent-filter>
</receiver>
</application>

最后放一个函数用来中断有序广播

public class MyR4eceive extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String message = intent.getStringExtra("MESSAGE");
        Log.i("godv", "MyR4eceive--接收广播" + message);
        //如果是有序广播
        if (isOrderedBroadcast()) {
            //中断
            abortBroadcast();
        }
    }

    public MyR4eceive() {
        Log.i("godv", "MyR4eceive--创建");
    }
}

 

在 Android 中发送有序广播,可以使用 `sendOrderedBroadcast()` 方法。这个方法会按照指定的顺序发送广播给所有的接收器,确保接收器的顺序是一致的,并且可以通过设置优先级来控制接收器的顺序。 下面是一个简单的例子: ```java Intent intent = new Intent("com.example.MY_ACTION"); intent.putExtra("message", "Hello, world!"); // 设置广播接收器的顺序 List<String> receiverPermissions = new ArrayList<>(); receiverPermissions.add("com.example.permission.RECEIVE_MY_ACTION"); receiverPermissions.add("android.permission.RECEIVE_MY_ACTION"); sendOrderedBroadcast(intent, receiverPermissions, new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // 处理接收到的广播 String message = intent.getStringExtra("message"); Log.d("MyApp", "Received broadcast message: " + message); } }, null, Activity.RESULT_OK, null, null); ``` 在这个例子中,我们创建了一个名为 `com.example.MY_ACTION` 的广播,并设置了一个包含消息的额外数据。然后我们调用 `sendOrderedBroadcast()` 方法,并传递接收器的顺序列表、一个广播接收器,以及其他参数。最后,我们在广播接收器的 `onReceive()` 方法中处理接收到的广播。 需要注意的是,发送有序广播需要指定接收器的顺序,这意味着你需要在应用程序的清单文件中为每个接收器定义一个优先级,以便系统可以按照正确的顺序发送广播
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值