What is the incoming call number String value when the call is withheld/unknown?

android

Solution

You can read through the source of `TelephonyRegistry`: Link

/** ... this class provides a centralized place that applications can register and be called back from. */

The broadcast for an incoming call is sent from this method:

private void broadcastCallStateChanged(int state, String incomingNumber) {
    ....
    ....
    Intent intent = new Intent(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
    intent.putExtra(PhoneConstants.STATE_KEY,
            DefaultPhoneNotifier.convertCallState(state).toString());

    // If `incomingNumber` is empty, the key 
    // `TelephonyManager.EXTRA_INCOMING_NUMBER` will not be present
    // in the attached Bundle
    if (!TextUtils.isEmpty(incomingNumber)) {
        intent.putExtra(TelephonyManager.EXTRA_INCOMING_NUMBER, incomingNumber);
    }
    mContext.sendBroadcastAsUser(intent, UserHandle.ALL,
            android.Manifest.permission.READ_PHONE_STATE);
}

So, you'll receive a value corresponding to `TelephonyManager.EXTRA_INCOMING_NUMBER` iff `incomingNumber` is not empty.

`incomingNumber` comes from `DefaultPhoneNotifier#notifyPhoneState(Phone)`: Link

@Override
public void notifyPhoneState(Phone sender) {
    Call ringingCall = sender.getRingingCall();
    String incomingNumber = "";
    if (ringingCall != null && ringingCall.getEarliestConnection() != null){

        // Here
        incomingNumber = ringingCall.getEarliestConnection().getAddress();

    }
    try {
        mRegistry.notifyCallState(convertCallState(sender.getState()), incomingNumber);
    } catch (RemoteException ex) {
        // system process is dead
    }
}

`ringingCall.getEarliestConnection().getAddress()` calls the `getAddress()` method from one of the classes that extend `com.android.internal.telephony.Connection` (GsmConnection, CdmaConnection..).

If you look at the constructor of `GsmConnection`, you can see that there are variables assigned for number AND caller name. From what I can tell, as a receiver, you only receive the number - if one is available. So, in case of an empty string (when a caller id is blocked/withheld), the key `TelephonyManager.EXTRA_INCOMING_NUMBER` will not be present in the `Bundle` you receive.

You did not mention as to why you need this information. If you are trying to show a personalized message by comparing String values, I suggest you do the following:

String phoneNumber = extras.getString(TelephonyManager.EXTRA_INCOMING_NUMBER,
                              "Personalized message for a Blocked Call");

I am not sure if this will be an adequate test, but to simulate a blocked caller id, you can take a look at this question: Link.

Problem

I have a `BroadcastReceiver` that listens for incoming calls. When a call is incoming, I get the number by doing the following: ``` Bundle extras = intent.getExtras(); ... String phoneNumber = extras.getString(TelephonyManager.EXTRA_INCOMING_NUMBER); ``` What happens when the incoming call is from a withheld number, and what value is assigned to the phoneNumber string? I'm guessing its either `null`, `"unknown"`, `"withheld"`, or something similar, but unfortunately i haven't found how to simulate incoming calls from a withheld number, so I cannot be sure.

Original source

Related problems