实现有序广播
首先我们需要注意以下几点:
- 第一点:广播的接受是按照事先设定的优先级接收的
- 第二点:优先级相同的广播接收者先被注册的先接收广播
- 第三点:如果广播在传递过程中被拦截,则后面的广播则无法接受广播
通过设置不同的优先级查看广播接受者的接受顺序,或通过不同的注册顺序查看广播接受者的接受顺序
首先先写基本的布局文件和广播接收器
布局文件:
<?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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:background="@drawable/stitch_one"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.bz0209.myapplication_3.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp"
android:onClick="send"
android:text="发送有序广播"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:background="#FBFBFF"
android:textSize="20sp"
/>
</RelativeLayout>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 1
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
Main_Activity:
package com.example.bz0209.myapplication_3;
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");
sendOrderedBroadcast(intent,null);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
MyReceiverone(其他两个只是输出的不一样其他一样):
package com.example.bz0209.myapplication_3;
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) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
Log.i("one", "one 接收到了事件");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bz0209.myapplication_3">
<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 android:priority="1000">
<action android:name="Intercept_Stitch"/>
</intent-filter>
</receiver>
<receiver
android:name=".MyReceivertwo"
android:enabled="true"
android:exported="true" >
<intent-filter android:priority="200">
<action android:name="Intercept_Stitch"/>
</intent-filter>
</receiver>
<receiver
android:name=".MyReceiverthree"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="600">
<action android:name="Intercept_Stitch"/>
</intent-filter>
</receiver>
</application>
</manifest>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
注意上面的Manifest文件中不同receiver的优先级
1. 正常运行测试:
我们通过日志的打印信息可以看到广播的接受是按照事先设定的优先级接收的。
2. 我们把MyReceivertwo的优先级同样设置成为1000并且把注册信息移动到MyReceiverone前面,运行效果如图:
我们发现优先级相同的广播接收者先被注册的先接收广播
3. 我们把MyReceivertwo修改为下面图片:
通
因为广播在MyReceivertwo被拦截,所以MyReceiverone和MyReceiverthree接收不到广播!!!
然后运行测试:
我们看到确实是这样。