Android 广播接收者练习-发送有序广播
任务内容:
一,发送有序广播对应的布局页面:
<?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_exe"
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:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/stitch_one"
tools:context="com.example.administrator.hello.Exe">
<Button
android:text="发送有序广播"
android:textSize="35dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:id="@+id/button" />
</RelativeLayout>
二、发送广播,当点击button按钮时,触发点击事件,会发送一条广播。
public class Exe extends AppCompatActivity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_exe);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new send());
}
private class send implements View.OnClickListener {
@Override
public void onClick(View v) {
Intent intent = new Intent("Intercept_Stitch");
sendOrderedBroadcast(intent,null);
}
}
}
三,接受广播,即添加广播接受者。
1.第一个接受者。
public class MyBroadReceiverOne extends BroadcastReceiver {
public MyBroadReceiverOne() {
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
Log.d("MyBroadReceiverOne","自定义的广播接受者01,接受到了自定义的广播事件");
}
}
2.第二个接受者。
public class MyBroadReceiverTwo extends BroadcastReceiver {
public MyBroadReceiverTwo() {
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
Log.d("MyBroadReceiverTwo","自定义的广播接受者02,接受到了自定义的广播事件");
}
}
3.第三个接受者
public class MyBroadReceiverThree extends BroadcastReceiver {
public MyBroadReceiverThree() {
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
Log.d("MyBroadReceiverThree","自定义的广播接受者03,接受到了自定义的广播事件");
}
}
四,三个广播接受者创建后,需要在清单文件中注册并设置他们的优先级。(android:priority属性指定了广播事件的优先级,优先级由高到底依次为:接受者01,接受者03,接受者02,并且要为这三个接受者定义监听的广播事件action为"Intercept_Stitch")
<receiver
android:name=".MyBroadReceiverOne"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="800">
<action android:name="Intercept_Stitch"/>
</intent-filter>
</receiver>
<receiver
android:name=".MyBroadReceiverTwo"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="200">
<action android:name="Intercept_Stitch"/>
</intent-filter>
</receiver>
<receiver
android:name=".MyBroadReceiverThree"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="600">
<action android:name="Intercept_Stitch"/>
</intent-filter>
</receiver>
所以当点击发送按钮时,按照优先级,他们的接受并弹出提示的顺序为01,03,02。