Android——发送广播(有序广播)

广播是一种可以跨进程的通信方式,这一点从前面接收系统广播的时候就可以看出来 了。因此在我们应用程序内发出的广播,其他的应用程序应该也是可以收到的。

1.我们需要再新建一个 BroadcastTest2项目。 将项目创建好之后,还需要在这个项目下定义一个广播接收器,用于接收上一小节中的 自定义广播。新建 AnotherBroadcastReceiver继承自 BroadcastReceiver,代码如下所示:

package com.example.broadcasttest2;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

import java.util.concurrent.ThreadPoolExecutor;

public class AnotherBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        Toast.makeText(context,"received in AnotherBroadcastReceiver",Toast.LENGTH_SHORT).show();
    }
}

2.。然后在 AndroidManifest.xml中对这个广播接收器进行注册,代码如下所示:

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <receiver
            android:name=".AnotherBroadcastReceiver"
            android:enabled="true"
            android:exported="true">
            //添加代码
            <intent-filter>
                <action android:name="com.example.broadcasttest.MY_BROADCAST"/>
            </intent-filter>
            //添加结束
        </receiver>

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

AnotherBroadcastReceiver 同 样 接 收 的 是 com.example.broadcasttest. MY_BROADCAST这条广播。现在运行 BroadcastTest2项目将这个程序安装到模拟器上,然 后重新回到 BroadcastTest项目的主界面,并点击一下 SendBroadcast按钮,就会分别弹出两 次提示信息。

现在我们来尝试一下发送有序广播。 关闭 BroadcastTest2项目,然后修改 MainActivity中的代码,如下所示:

package com.example.broadcasts_demo;


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


public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button=findViewById(R.id.btn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent("com.example.broadcasttest.MY_BROADCAST");
                //发送一条有序广播
                sendOrderedBroadcast(intent,null);
            }
        });
    }


}

可以看到,发送有序广播只需要改动一行代码,即将 sendBroadcast()方法改成 sendOrderedBroadcast()方法。sendOrderedBroadcast()方法接收两个参数,第一个参数仍然是 Intent,第二个参数是一个与权限相关的字符串,这里传入 null就行了。

看上去好像和标准广播没什么区别嘛,不过别忘了,这个时候的广播接收器是有先后顺 序的,而且前面的广播接收器还可以将广播截断,以阻止其继续传播。 那么该如何设定广播接收器的先后顺序呢?当然是在注册的时候进行设定的了,修改 AndroidManifest.xml中的代码,如下所示:

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <receiver
            android:name=".MyBroadcastReceiver"
            android:enabled="true"
            android:exported="true">
            //代码修改
            <intent-filter android:priority="100">
            //修改结束
                <action android:name="com.example.broadcasttest.MY_BROADCAST"/>
            </intent-filter>
        </receiver>

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

可以看到,我们通过 android:priority属性给广播接收器设置了优先级,优先级比较高的 广播接收器就可以先收到广播。这里将 MyBroadcastReceiver的优先级设成了 100,以保证它 一定会在 AnotherBroadcastReceiver之前收到广播。

既然已经获得了接收广播的优先权,那么 MyBroadcastReceiver就可以选择是否允许广 播继续传递了。修改 MyBroadcastReceiver中的代码,如下所示:

package com.example.broadcasts_demo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class MyBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context,"received in MyBroadcastReceiver",Toast.LENGTH_SHORT).show();
        //将广播截断
        abortBroadcast();
    }
}

如果在 onReceive()方法中调用了 abortBroadcast()方法,就表示将这条广播截断,后面的 广播接收器将无法再接收到这条广播。现在重新运行程序,并点击一下 SendBroadcast按钮, 你会发现,只有 MyBroadcastReceiver 中的 Toast 信息能够弹出,说明这条广播经过 MyBroadcastReceiver之后确实是终止传递了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值