[英]Android 9+ doesn't provide the incoming phone number
在 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 上运行的应用程序无法读取电话号码或电话状态,除非首先获得
READ_CALL_LOG
权限。与传入和传出呼叫关联的电话号码在电话状态广播中可见,例如传入和传出呼叫,并且可以从
PhoneStateListener
类访问。 但是,如果没有READ_CALL_LOG
权限,在PHONE_STATE_CHANGED
广播中和通过PhoneStateListener
提供的电话号码字段为空。
它继续说明需要进行哪些更改:
要从手机状态读取电话号码,请更新您的应用以根据您的用例请求必要的权限:
- 要从
PHONE_STATE
意图操作读取数字,您需要READ_CALL_LOG
权限和READ_PHONE_STATE
权限。- 要从
onCallStateChanged()
读取数字,您只需要READ_CALL_LOG
权限。 您不需要READ_PHONE_STATE
权限。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.