1 发广播
Intent intent = new Intent("顺序广播的Action")
sendOrderedBroadcast(intent,null);
2 编写广播类(多个)
class R1 extends BroadcastReceiver{
Log.i("r1", "r1");
}
class R2 extends BroadcastReceiver{
Log.i("r2", "r2");
}
class R3 extends BroadcastReceiver{
Log.i("r3", "r3");
}
3 在onStart中注册顺序广播
r1 = new R1();
IntentFilter f1 = new IntentFilter("广播的Action")
在意图过滤器中设置级别 数字高的先执行
f1.setPriority(数字)
this.registerReceiver(r1, f1);
4 在onStop()中关闭注册
unregisterReceiver(r1);
源码:
package com.example.d08orderbroadcast;
import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
private R1 r1;
private R2 r2;
private IntentFilter if2;
private R3 r3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void doClick(View v){
Intent intent = new Intent("AA");
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;
}
class R1 extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.i("r1", "r1");
}
}
class R2 extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.i("r2", "r2");
this.abortBroadcast();
}
}
class R3 extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.i("r3", "r4");
}
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
r1 = new R1();
IntentFilter if1 = new IntentFilter("AA");
if1.setPriority(111);
this.registerReceiver(r1, if1);
r2 = new R2();
IntentFilter if2 = new IntentFilter("AA");
if2.setPriority(222);
this.registerReceiver(r2, if2);
r3 = new R3();
IntentFilter if3 = new IntentFilter("AA");
if3.setPriority(333);
this.registerReceiver(r3, if3);
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
unregisterReceiver(r1);
unregisterReceiver(r2);
unregisterReceiver(r3);
super.onStop();
}
}
布局:
<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_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="106dp"
android:text="Button"
android:onClick="doClick"/>
</RelativeLayout>
Intent intent = new Intent("顺序广播的Action")
sendOrderedBroadcast(intent,null);
2 编写广播类(多个)
class R1 extends BroadcastReceiver{
Log.i("r1", "r1");
}
class R2 extends BroadcastReceiver{
Log.i("r2", "r2");
}
class R3 extends BroadcastReceiver{
Log.i("r3", "r3");
}
3 在onStart中注册顺序广播
r1 = new R1();
IntentFilter f1 = new IntentFilter("广播的Action")
在意图过滤器中设置级别 数字高的先执行
f1.setPriority(数字)
this.registerReceiver(r1, f1);
4 在onStop()中关闭注册
unregisterReceiver(r1);
源码:
package com.example.d08orderbroadcast;
import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
private R1 r1;
private R2 r2;
private IntentFilter if2;
private R3 r3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void doClick(View v){
Intent intent = new Intent("AA");
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;
}
class R1 extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.i("r1", "r1");
}
}
class R2 extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.i("r2", "r2");
this.abortBroadcast();
}
}
class R3 extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.i("r3", "r4");
}
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
r1 = new R1();
IntentFilter if1 = new IntentFilter("AA");
if1.setPriority(111);
this.registerReceiver(r1, if1);
r2 = new R2();
IntentFilter if2 = new IntentFilter("AA");
if2.setPriority(222);
this.registerReceiver(r2, if2);
r3 = new R3();
IntentFilter if3 = new IntentFilter("AA");
if3.setPriority(333);
this.registerReceiver(r3, if3);
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
unregisterReceiver(r1);
unregisterReceiver(r2);
unregisterReceiver(r3);
super.onStop();
}
}
布局:
<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_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="106dp"
android:text="Button"
android:onClick="doClick"/>
</RelativeLayout>