0

I have an app that checks the phone number of an incoming call against a blacklist.

I have used the below code for several versions of Android to get the phone number of an incoming call, but when I test it against Android P, it behaves unexpectedly.

For readability, I have removed all null checks from the code below.

public class IncomingCallHandler extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        String state = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);

        if (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING)) {

            String phoneNumber = bundle
                .getString(TelephonyManager.EXTRA_INCOMING_NUMBER);

        }
    }
}

On Android versions less than P, onReceive with state EXTRA_STATE_RINGING may be called several times during an incoming call, but phoneNumber always has the same value (the actual incoming phone number).

On Android P, onReceive is called twice during an incoming call. The first time phoneNumber=null, the second time it is the actual phone number.

Is this a bug? Is it supposed to be like this? Do you get the same in your apps?

Bjarte Aune Olsen
  • 3,230
  • 4
  • 24
  • 36
  • 1
    There was one behavior change regarding permissions, [link](https://developer.android.com/preview/behavior-changes). Didn't check personally but maybe first broadcast is for apps without permission? – Pawel Jul 24 '18 at 20:20
  • 1
    There's also issue about this with some information; https://issuetracker.google.com/issues/110285356 . – harism Jul 24 '18 at 20:24
  • Thanks so much, both of you. From the discussion above, it seems this is the correct behaviour, and it should be consistent. Pawel, you're right, the first broadcast is for apps without READ_CALL_LOG permission. – Bjarte Aune Olsen Jul 24 '18 at 20:29
  • So what is the solution for that? I have an application which is running on android 8 and also in android9 and if i don't include READ_CALL_LOG permission i am getting null value from intent TelephonyManager.EXTRA_INCOMING_NUMBER and by adding this permission getting a twice call. What should i do to prevent twice call? – user565 Sep 16 '19 at 10:51
  • @user565 The solution is to expect the two calls. You cannot prevent it, but you can just ignore the first call and get the incoming phone number from the second call. – Bjarte Aune Olsen Sep 17 '19 at 13:51

0 Answers0