@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"/>
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 theREAD_CALL_LOG
permission, however, the phone number field that's provided inPHONE_STATE_CHANGED
broadcasts and throughPhoneStateListener
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 theREAD_CALL_LOG
permission only. You don't need theREAD_PHONE_STATE
permission.