当前位置: 代码迷 >> 综合 >> android 有序广播
  详细解决方案

android 有序广播

热度:79   发布时间:2024-03-08 12:42:06.0

android:priority="100",数字越高优先级越高越先执行

        <receiver android:name="com.sql.broadcast.MyReceiver1"><intent-filter android:priority="1"><action android:name="com.ty.OrderedBroadcast"/></intent-filter></receiver><receiver android:name="com.sql.broadcast.MyReceiver2"><intent-filter android:priority="0"><action android:name="com.ty.OrderedBroadcast"/></intent-filter></receiver>
public class MyReceiver1 extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {Toast.makeText(context, "接收到的Intent的Action为:" + intent.getAction() + "\n 消息内容是:" + intent.getStringExtra("msg"), Toast.LENGTH_LONG).show();Bundle bundle = new Bundle();bundle.putString("first", "aaaa");setResultExtras(bundle);//abortBroadcast();}
}
public class MyReceiver2 extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {Bundle bundle = getResultExtras(true);//解析前一个BroadcastReceiver所存入的key为first的消息String first = bundle.getString("first");Toast.makeText(context, "我是第二个BroadcastReceiver,我收到的消息是:" + first, Toast.LENGTH_LONG).show();}
}

MainActivity.java

    public void sendOrderBroadcast(){Intent intent = new Intent();intent.setAction("com.ty.OrderedBroadcast");intent.putExtra("msg","111111");sendOrderedBroadcast(intent, null);}

 

  相关解决方案