//点击按钮 发送大米 public void click(View v) { //发送一条有序广播 Intent intent = new Intent(); //设置一个action intent.setAction("com.itheima.rice"); /** * receiverPermission 接收的权限 * resultReceiver 最为一个最终的receive 最后都能够接收到广播 */ sendOrderedBroadcast(intent, null, new FinalReceiver(), null, 1, "给每个村民发了1000斤大米", null); }
import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class ProvienceReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { //[1]获取到发送广播携带的数据 String content = getResultData(); //[2]展示到Toast上 Toast.makeText(context, "省:"+content, 1).show(); //[2.1]终止广播 // abortBroadcast(); //[3]修改数据 (扣留大米) setResultData("给每个村民发了500斤大米"); } }
import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class CityReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { //[1]获取到发送广播携带的数据 String content = getResultData(); //[2]展示到Toast上 Toast.makeText(context, "市:"+content, 1).show(); //[3]修改数据 (扣留大米) setResultData("给每个村民发了200斤大米"); } }
import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class CountryReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { //[1]获取到发送广播携带的数据 String content = getResultData(); //[2]展示到Toast上 Toast.makeText(context, "乡:"+content, 1).show(); //[3]修改数据 (扣留大米) setResultData("给每个村民发了10斤大米"); } }
import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class NongMinReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { //[1]获取到发送广播携带的数据 String content = getResultData(); //[2]展示到Toast上 Toast.makeText(context, "农民:"+content, 1).show(); } }
import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; /** * 最终的receiver 不需要再清单文件里面配置 * @author jhon * */ public class FinalReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String content = getResultData(); Toast.makeText(context, "报告"+content, 1).show(); } }
<!--配置广播接收者 --> <receiver android:name="com.itheima.receiverice.ProvienceReceiver"> <intent-filter android:priority="1000" > <action android:name="com.itheima.rice"/> </intent-filter> </receiver> <receiver android:name="com.itheima.receiverice.CityReceiver"> <intent-filter android:priority="800" > <action android:name="com.itheima.rice"/> </intent-filter> </receiver> <receiver android:name="com.itheima.receiverice.CountryReceiver"> <intent-filter android:priority="100" > <action android:name="com.itheima.rice"/> </intent-filter> </receiver> <receiver android:name="com.itheima.receiverice.NongMinReceiver"> <intent-filter android:priority="10" > <action android:name="com.itheima.rice"/> </intent-filter> </receiver>