androidphone-state-listener

PhoneStateListener onCallStateChanged method params "incoming number" empty in Android 9.0?


@Override
public void onCallStateChanged(int state, String incomingNumber) {
    super.onCallStateChanged(state, incomingNumber);
    switch (state) {
        case TelephonyManager.CALL_STATE_RINGING:
            if (incomingNumber == null || "".equals(incomingNumber)) {
                return;
            }
            break;
    }
}

I got same issue below Android sdk27 ever, I start PhoneStateListener in PhoneService ,

public  void startPhoneStateListener() {
    mTelManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    mTelManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}

but I only start Service in RootReceiver, not in MainActivity ,when I start PhoneService in MainActivity ,I fixed it.

  <receiver android:name=".receiver.RootReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
        </intent-filter>
    </receiver>

but this only work below Android sdk 27 , I have no idea about Android 9.0 , by the way, I have written the permission :

 <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

Solution

  • As per the Behavior Changes for all apps in Android 9.0:

    Phone numbers associated with incoming and outgoing calls are visible in the phone state broadcast, such as for incoming and outgoing calls and are accessible from the PhoneStateListener class. Without the READ_CALL_LOG permission, however, the phone number field that's provided in PHONE_STATE_CHANGED broadcasts and through PhoneStateListener is empty.

    You must request the READ_CALL_LOG permission if you want the incoming phone number.

    Note that as per that same page:

    To read numbers from onCallStateChanged(), you need the READ_CALL_LOG permission only. You don't need the READ_PHONE_STATE permission.