心中的疑惑:两个BroadcastReceiver优先级相同时,请会先接收到广播?
小测试:
//主界面,点击主界面的按钮后,会发送一条有序广播
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View view) {
//点击按钮后,发送一条有序广播
Intent intent = new Intent();
intent.setAction("cn.melon");
sendOrderedBroadcast(intent, null);
}
}
//MyReceiver1,接收到广播信息后,在控制台打印一条语句,显示是本Receiver接收
public class MyReceiver1 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("MyReceiver1接收到广播: "+intent.getAction());
}
}
//MyReceiver2,接收到广播信息后,在控制台打印一条语句,显示是本Receiver接收
public class MyReceiver2 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("MyReceiver2接收到广播: "+intent.getAction());
}
}
//配置清单主代码
<receiver android:name=".MyReceiver2">
<intent-filter android:priority="1000">
<action android:name="cn.melon"/>
</intent-filter>
</receiver>
<receiver android:name=".MyReceiver1">
<intent-filter android:priority="1000">
<action android:name="cn.melon"/>
</intent-filter>
</receiver>
当两个Receive的在配置清单的配置调换一下位置时候:
<receiver android:name=".MyReceiver1">
<intent-filter android:priority="1000">
<action android:name="cn.melon"/>
</intent-filter>
</receiver>
<receiver android:name=".MyReceiver2">
<intent-filter android:priority="1000">
<action android:name="cn.melon"/>
</intent-filter>
</receiver>
结果:
由此推测:优先级相同时,哪个Receiver先在Android系统中先注册,哪个先接收到广播。