Android 9+ 不提供传入电话号码



在Android 8.0中,我会收到传入的电话号码。我请求READ_PHONE_STATE权限,用户必须为应用程序授予该权限。

然而,同样的事情在Android 9.0上不起作用(在多台设备上测试(。

两个版本的代码相同。

我该怎么做才能得到这个?这是代码

package io.gvox.phonecalltrap;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.PluginResult;
import android.content.Context;
import android.Manifest;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import org.json.JSONException;
import org.json.JSONArray;

public class PhoneCallTrap extends CordovaPlugin {
CallStateListener listener;
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
prepareListener();
listener.setCallbackContext(callbackContext);
return true;
}
private void prepareListener() {
if (listener == null) {
listener = new CallStateListener();
TelephonyManager TelephonyMgr = (TelephonyManager) cordova.getActivity().getSystemService(Context.TELEPHONY_SERVICE);
TelephonyMgr.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
}
}
}
class CallStateListener extends PhoneStateListener {
private CallbackContext callbackContext;
public void setCallbackContext(CallbackContext callbackContext) {
this.callbackContext = callbackContext;
}
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
if (callbackContext == null) return;
String msg = "";
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
msg = "IDLE|" + incomingNumber;
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
msg = "OFFHOOK|" + incomingNumber;
break;
case TelephonyManager.CALL_STATE_RINGING:
msg = "RINGING|" + incomingNumber;
break;
}
PluginResult result = new PluginResult(PluginResult.Status.OK, msg);
result.setKeepCallback(true);
callbackContext.sendPluginResult(result);
}
}

注意:我确实收到了呼叫的状态(IDLE、OFFHOOK、RINGING…(,但没有收到电话号码

根据Android 9.0行为更改:

在Android 9上运行的应用程序在未首先获得READ_CALL_LOG权限的情况下,除了应用程序用例所需的其他权限外,无法读取电话号码或电话状态。

与传入和传出呼叫相关联的电话号码在电话状态广播中可见,例如用于传入和传出的呼叫,并且可以从PhoneStateListener类访问。然而,在没有READ_CALL_LOG许可的情况下,PHONE_STATE_CHANGED广播和通过PhoneStateListener提供的电话号码字段为空。

它继续说明需要进行哪些更改:

要从手机状态读取电话号码,请更新您的应用程序以根据您的用例请求必要的权限:

  • 要从PHONE_STATEintent操作读取数字,您需要READ_CALL_LOG权限和READ_PHONE_STATE权限
  • 要从onCallStateChanged()读取数字,您只需要READ_CALL_LOG权限。您不需要READ_PHONE_STATE权限

最新更新