Android mistake with Broadcast / Receivers

二次信任 提交于 2020-03-23 12:33:13

问题


I'm using two receivers. One declared in the manifest like this :

    <receiver
        android:name="com.app.receivertest"
        android:exported="false"
        android:enabled="true"
        android:permission="MY_PERMISSION">
        <intent-filter android:priority="0">
            <action android:name="NAME_ACTION" />
        </intent-filter>
    </receiver>

And one declared in my activity A, declared with priority 1.

When I start my service on this activity A, on Android 4.4.2, both receivers are called, in the "correct order" whereas in Android 5 > 0, just the correct receiver declared in the activity A is called, not the manifest's receiver.

This is my code in the receiver used in the activity a :

     @Override
    public void onReceive(Context context, Intent intent) {
        Utils.log("onReceive test");
        abortBroadcast();
        setResultData("test");
        myfunction(); 
    }

after this code, the other receiver is called. I tried to use "setResultData", to use "getResultData" in the other receiver for don't execute the code if I've something, but getResultData returns always null..

So, why both receivers are called ? What happens on Android 4.4 ? I need your help :D

EDIT : Code for sending broadcast :

 Intent broadcastIntent = new Intent();
    broadcastIntent.setAction(My_ACTION);
    sendOrderedBroadcast(broadcastIntent,   My_PERMISSION);

来源:https://stackoverflow.com/questions/32696013/android-mistake-with-broadcast-receivers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!