@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; } }
我在 Android sdk27 以下遇到过同样的问题,我在 PhoneService 中启动 PhoneStateListener,
public void startPhoneStateListener() { mTelManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); mTelManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE); }
但我只在 RootReceiver 中启动服务,而不是在 MainActivity 中启动服务,当我在 MainActivity 中启动 PhoneService 时,我修复了它。
<receiver android:name=".receiver.RootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
但这只适用于 Android sdk 27 以下,我不知道 Android 9.0,顺便说一句,我已经写了权限:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
最佳答案
根据 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
PhoneStateListenerclass. Without theREAD_CALL_LOGpermission, however, the phone number field that's provided inPHONE_STATE_CHANGEDbroadcasts and throughPhoneStateListeneris empty.
如果您想要来电号码,您必须请求 READ_CALL_LOG 权限。
请注意,根据同一页面:
To read numbers from
onCallStateChanged(), you need theREAD_CALL_LOGpermission only. You don't need theREAD_PHONE_STATEpermission.
关于android - PhoneStateListener onCallStateChanged 方法参数 "incoming number"在 Android 9.0 中为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54977469/