Priority in a broadcast receiver to receive calls

前端 未结 4 1805
时光取名叫无心
时光取名叫无心 2020-12-06 13:00

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 rece

相关标签:
4条回答
  • 2020-12-06 13:27

    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.

    0 讨论(0)
  • 2020-12-06 13:30

    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?

    0 讨论(0)
  • 2020-12-06 13:37

    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

    0 讨论(0)
  • 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" />
    
    0 讨论(0)
提交回复
热议问题