BroadcastReceiver的优先级是可以设置的:如下:
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter android:priority="9">
<action android:name="com.example.yabushan.broadercastreceiver.intent.action.MyReceiver" />
</intent-filter>
</receiver>
<receiver
android:name=".MyReceiver1"
android:enabled="true"
android:exported="true" >
<intent-filter android:priority="10">
<action android:name="com.example.yabushan.broadercastreceiver.intent.action.MyReceiver" />
</intent-filter>
</receiver>
除了可以设置优先级外,还可以设置优先级高的broadcastReceiver执行完后,中断优先级低的,让优先级不能获取执行权限:
package com.example.yabushan.broadercastreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
//第一个broadcastReceiver
public class MyReceiver extends BroadcastReceiver {
// public final String ACTION="com.example.yabushan.broadercastreceiver.intent.action.MyReceiver";
public MyReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("接收到广播数据:"+intent.getStringExtra("DATA").toString());
}
}
package com.example.yabushan.broadercastreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
//第二个BroadercastReceiver
public class MyReceiver1 extends BroadcastReceiver {
public MyReceiver1() {
}
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("MyReceiverl 接收到了信息。");
//在androidmanifest.xml中设置该broadcastreceiver的优先级高
abortBroadcast();
}
}
package com.example.yabushan.broadercastreceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.deleverData).setOnClickListener(this);
findViewById(R.id.reg).setOnClickListener(this);
findViewById(R.id.unreg).setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.deleverData:
//这里需要使用隐示Intent
// Intent intent=new Intent(this,MyReceiver.class);
// System.out.println(myReceiver.ACTION);
Intent intent=new Intent("com.example.yabushan.broadercastreceiver.intent.action.MyReceiver");
intent.putExtra("DATA","您好");
// sendBroadcast(intent);
//这里需要用排序的发送方式
sendOrderedBroadcast(intent,null);
break;
case R.id.reg:
//注册
if(myReceiver==null){
myReceiver =new MyReceiver();
registerReceiver(myReceiver,new IntentFilter(myReceiver.ACTION));
}
break;
case R.id.unreg:
if(myReceiver!=null){
//解除注册
unregisterReceiver(myReceiver);
}
break;
}
}
private MyReceiver myReceiver=null;
}
使用相同的action!<?xml version="1.0" encoding="utf-<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.yabushan.broadercastreceiver" >
<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=".MyReceiver" android:enabled="true" android:exported="true" > <intent-filter android:priority="9">
<action android:name="com.example.yabushan.broadercastreceiver.intent.action.MyReceiver" /> </intent-filter> </receiver> <receiver android:name=".MyReceiver1" android:enabled="true" android:exported="true" > <intent-filter android:priority="10"> <action android:name="com.example.yabushan.broadercastreceiver.intent.action.MyReceiver" /> </intent-filter> </receiver> </application> </manifest>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <TextView android:text="Hello World!" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="传递消息" android:id="@+id/deleverData" android:layout_centerVertical="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="注册" android:id="@+id/reg" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="注销" android:id="@+id/unreg" /> </LinearLayout>