美文网首页
BroadcastReceiver的优先级

BroadcastReceiver的优先级

作者: csp | 来源:发表于2017-03-06 15:08 被阅读187次

    有多个BroadcastReceiver的时候设置优先级,可以在manifest文件中设置优先级,优先级越高,数字越大:
    <receiver
    android:name=".MyReceiver"
    android:enabled="true"
    android:exported="true">
    <intent-filter android:priority="9">
    <action android:name="com.chenshipeng.learnbroadcastreceiver.intent.action.MyReceiver" />
    </intent-filter>
    </receiver>
    <receiver
    android:name=".MyReceiver1"
    android:enabled="true"
    android:exported="true">
    <intent-filter android:priority="8">
    <action android:name="com.chenshipeng.learnbroadcastreceiver.intent.action.MyReceiver" />
    </intent-filter>
    </receiver>
    在高优先级的BroadcastReceiver中可以中断后续的broadcastReceiver:
    public class MyReceiver extends BroadcastReceiver {

    public  static final  String ACTION = "com.chenshipeng.learnbroadcastreceiver.intent.action.MyReceiver";
    @Override
    public void onReceive(Context context, Intent intent) {
    
    
    //        System.out.println("接收到了消息,消息的内容是:"+intent.getStringExtra("data"));
        System.out.println("MyReceiver接收到了消息");
        abortBroadcast();
    }
    

    }
    当然这只有当发送消息的时候使用的是:
    sendOrderedBroadcast(i,null);
    而不是
    sendBroadcast(i);的时候。

    相关文章

      网友评论

          本文标题:BroadcastReceiver的优先级

          本文链接:https://www.haomeiwen.com/subject/avcugttx.html