发送有序广播

   一、

1.有序广播

        有序广播是一种同步执行的广播,在广播发出之后,同一时刻只会有一个广播接收器能够接收到这条消息,当这个广播接收器中的逻辑执行完毕后,广播才会继续传递。所以此时的广播接收器是有先后顺序的,并且可以被拦截。

2.创建应用程序

对应的布局文件 activity_main.xml  代码如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/stitch_one"
    tools:context="cn.edu.bzu.myapplication.MainActivity">

<Button
     android:id="@+id/btn1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="发送有序广播"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="100dp"
    android:textSize="20sp"
    android:background="#FBFBFF"
    android:onClick="send"/>

</RelativeLayout>
3.在布局文件中为按钮Button注册了一个事件send,当点击按钮时,会发送一条有序广播。MainActivity的代码如下:

package cn.edu.bzu.myapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void send(View view){
        Intent intent=new Intent();
        intent.setAction("intercept_stitch");
        sendBroadcast(intent,null);
    }
}
4.添加三个广播接收者MyReceiverOne、MyReceiverTwo、MyReceiverThree。不同的广播接收者打印不同的提示信息。

(1)MyReceiverOne的代码如下:

package cn.edu.bzu.myapplication;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class MyReceiverOne extends BroadcastReceiver {
    public MyReceiverOne() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("MyReceiverOne","自定义的广播接收者one,接收到了广播事件");

    }
}

(2)MyReceiverTwo的代码如下:

package cn.edu.bzu.myapplication;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class MyReceiverTwo extends BroadcastReceiver {
    public MyReceiverTwo() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("MyReceiverTwo","自定义广播接收者Two,已接受到了广播事件");

    }
}

(3)MyReceiverThree的代码如下:

package cn.edu.bzu.myapplication;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class MyReceiverThree extends BroadcastReceiver {
    public MyReceiverThree() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("MyreceiverThree","自定义广播接收者Three,已接收到了广播事件");
    }
}


5.在清单文件中注册并为它们指定不同的优先级,代码如下:
<receiver android:name="cn.edu.bzu.myapplication.MyReceiverOne" >
    <intent-filter android:priority="1000">
        <action android:name="intercept_stitch"></action>
    </intent-filter>
</receiver>
<receiver android:name="cn.edu.bzu.myapplication.MyReceiverTwo"
    >
    <intent-filter android:priority="200">
        <action android:name="intercept_stitch" ></action>
    </intent-filter>

</receiver>
<receiver android:name="cn.edu.bzu.myapplication.MyReceiverThree"
    >
    <intent-filter android:priority="600">
        <action android:name="intercept_stitch" ></action>
    </intent-filter>
</receiver>

 

在 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、付费专栏及课程。

余额充值