package com.example.broadcasttest3; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class AnotherBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context,Intent intend) { Toast.makeText(context, "receive in AnotherBroadcastReceiver",Toast.LENGTH_SHORT).show(); } }
<application ...................省略代码................... <receiver android:name=".AnotherBroadcastReceiver"> <intent-filter> <action android:name="com.example.broadcasttest.MY_BROADCAST" /> </intent-filter> </receiver> </application>
com.example.broadcasttest.MY_BROADCAST
的广播send broadcast
按钮,会出现注意:
com.example.broadcasttest.MY_BROADCAST
的广播已经在第一个项目的Androidmanifest.xml
文件中定义好了。
MainActivity.java
中//将sendBroadcast(intent);修改为如下有序广播的方法 sendOrderedBroadcast(intent,null);
AndroidManifest.xml
中文件添加优先级,代表项目1,在项目2之前获得该广播<receiver android:name=".MyBroadcastReceiver"> <intent-filter android:priority="100"> <action android:name="com.example.broadcasttest.MY_BROADCAST"/> </intent-filter> </receiver>
android:priority="100"
MyBroadcastReceiver
public void onReceive(Context context,Intent intent) { Toast.makeText(context, "received in MyBroadcastReceiver", Toast.LENGTH_SHORT).show(); abortBroadcast(); }
LocalBroadcastManager
来管理原文:https://www.cnblogs.com/ruigege0000/p/13258557.html