Priority in a broadcast receiver to receive calls

谁说胖子不能爱 提交于 2019-12-17 16:37:44

问题


My intention is to make a Broadcast receiver that performs actions when receiving a call. Is it possible that had more priority than the automatic call reception SO?.

I've tried assigning a priority of 2147483647 which I think is the best, but still jumps me to try the call before the end of my receiver.

<!-- Receiver de llamadas -->
<receiver android:name=".PhoneCall">
    <intent-filter android:priority="2147483647">
        <action android:name="android.intent.action.PHONE_STATE"/>   
    </intent-filter>
</receiver>

回答1:


This link answer me:

http://developer.android.com/reference/android/content/BroadcastReceiver.html

There are two major classes of broadcasts that can be received:

  • Normal broadcasts (sent with Context.sendBroadcast) are completely asynchronous. All receivers of the broadcast are run in an undefined order, often at the same time. This is more efficient, but means that receivers cannot use the result or abort APIs included here.

  • Ordered broadcasts (sent with Context.sendOrderedBroadcast) are delivered to one receiver at a time. As each receiver executes in turn, it can propagate a result to the next receiver, or it can completely abort the broadcast so that it won't be passed to other receivers. The order receivers run in can be controlled with the android:priority attribute of the matching intent-filter; receivers with the same priority will be run in an arbitrary order.

Broadcast like PHONE_STATE are "Normal broadcast". As far as I understand it is not possible to prioritize my broadcast. Does anyone think of any way?




回答2:


Actually, I don't think that 2147483647 is the best value to use, because Android won't understand it and will ignore this value. What you have to do is try to set the priority to 999, since I guess 1000 is the maximum value.




回答3:


The value should be less the 1000!

Android developer docs:

The value must be an integer, such as "100". Higher numbers have a higher priority. The default value is 0. The value must be greater than -1000 and less than 1000.

http://developer.android.com/guide/topics/manifest/intent-filter-element.html




回答4:


My solution is creating two broadcast receivers. The first receiver is used to receive the broadcast sent by the system with Action: android.intent.action.PHONE_STATE. The second receiver is called by the first receiver. (The first receiver will send a broadcast and I find that this broadcast will be received by the second receiver after all receivers receive android.intent.action.PHONE_STATE.)

The detailed codes are show below: The first receiver (CallReceiver.java):

public class CallReceiver extends BroadcastReceiver {   
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent newintent = new Intent(intent);
        newintent.setAction("");
        newintent.setClass(context, SecondReceiver.class);
        context.sendBroadcast(newintent);
    }
}

The second receiver (SecondReceiver.java):

public class SecondReceiver extends BroadcastReceiver{  
    @Override
    public void onReceive(Context context, Intent intent) {
        String number = intent.getStringExtra(
        TelephonyManager.EXTRA_INCOMING_NUMBER);
        String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
    }
}

AndroidManifest.xml:

<receiver android:name=".CallReceiver" >
    <intent-filter>
        <action android:name="android.intent.action.PHONE_STATE" />
    </intent-filter>
</receiver>

<receiver android:name=".SecondReceiver" />


来源:https://stackoverflow.com/questions/5817178/priority-in-a-broadcast-receiver-to-receive-calls

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