给多个广播接收者发送广播、有序广播的使用在安卓开发中是经常使用的方式,下面我们来介绍一下它的使用方法。
整体思路:在xml文件中放置两个Button控件,给这两个Button控件设置点击事件,在第一个点击事件中传递一个数据,设置一个动作并发送广播,在第二个点击事件中传递一个数据,设置一个动作并发送有序广播。新建三个继承BroadcastReceiver的广播接收类,用于接收传递的数据。在第二个BroadcastReceiver子类中停止广播的接收,发现在发送有序广播的时候会影响后续广播接收者进行接收广播。在清单文件注册者三个广播接收类,并设置它们的优先级和行为。
activity_main.xml文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="41dp"
android:text="给多个广播接收者发送广播" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginTop="53dp"
android:text="发送有序广播" />
</RelativeLayout>
Broadcast1.java文件:
package com.example.android_broadcast_order;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class Broadcast1 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
System.out.println("Broadcast1-->"+intent.getStringExtra("name"));
}
}
Broadcast2.java文件:
package com.example.android_broadcast_order;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class Broadcast2 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
System.out.println("Broadcast2-->"+intent.getStringExtra("name"));
abortBroadcast();//结束广播,如果是有序广播的话则不继续发送后面的广播
}
}
Broadcast3.java文件:package com.example.android_broadcast_order;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class Broadcast3 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
System.out.println("Broadcast3-->"+intent.getStringExtra("name"));
}
}
MainActivity.java文件:
package com.example.android_broadcast_order;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
private Button button;
private Button button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.button1);
button2=(Button)findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
// 给多个广播接收者发送广播
Intent intent=new Intent();
intent.putExtra("name", "jack");
intent.setAction("com.mybroadcast.sender");//设置一个动作
sendBroadcast(intent);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent=new Intent();
intent.putExtra("name", "rose");
intent.setAction("com.mybroadcast.sender");//设置一个动作
// 发送有序广播
sendOrderedBroadcast(intent, null);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
AndroidManifest.xml文件:
<!-- 发送广播的顺序会根据优先级的大小 优先级大的先发送-->
<receiver android:name="com.example.android_broadcast_order.Broadcast1">
<intent-filter android:priority="999">
<action android:name="com.mybroadcast.sender"/>
</intent-filter>
</receiver>
<receiver android:name="com.example.android_broadcast_order.Broadcast2">
<intent-filter android:priority="1000">
<action android:name="com.mybroadcast.sender"/>
</intent-filter>
</receiver>
<receiver android:name="com.example.android_broadcast_order.Broadcast3">
<intent-filter android:priority="998">
<action android:name="com.mybroadcast.sender"/>
</intent-filter>
</receiver>