PhoneStateListener onCallStateChanged 方法参数 "incoming number" 在 Android 9.0 中为空?

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;
    }
}

我在 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 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.

如果您想要传入的 phone 号码,您必须请求 READ_CALL_LOG 权限。

请注意,根据同一页面:

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