gpt4 book ai didi

java - 如何在 Android Q 中检测拨出电话号码

转载 作者:行者123 更新时间:2023-12-02 09:43:04 25 4
gpt4 key购买 nike

我无法在 Android Q 中获取拨出电话号码。

我已经使用此 Intent 过滤器在 list 中注册了接收器android.intent.action.NEW_OUTGOING_CALL,并且在代码中我正在检测这样的传出电话号码

@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL"))
String nr = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
}

但我永远无法在 Android Q 中获取拨出电话号码,是否有解决方法可以以不同方式获取此号码,或者由于 Android Q 完全不可能检测到拨出电话号码电话号码?

编辑:它适用于以前的 Android 版本

最佳答案

您需要添加 PROCESS_OUTGOING_CALLS 权限

创建 OutgoingCallReceiver

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;

public class OutgoingCallReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

TelephonyManager tm = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);
if (tm.getCallState() == TelephonyManager.CALL_STATE_OFFHOOK) {

String number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);

}

}

}

在 AndroidManifest 文件中添加读取拨出调用所需的权限

  <uses-permission android:name="android.permission.NEW_OUTGOING_CALL" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

在运行时请求权限

if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.PROCESS_OUTGOING_CALLS, Manifest.permission.READ_PHONE_STATE},
1);
}

在AndroidManifest文件中添加OutgoingCallReceiver

  <receiver
android:name=".application.services.OutgoingCallReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>

此代码可以正常工作,但是当您需要在 Google Play 上上传应用程序时,NEW_OUTGOING_CALL 和 READ_PHONE_STATE 权限是可以的,但是,您将收到来自 playStore 的政策通知:

您的应用 list 请求调用日志权限组(例如 PROCESS_OUTGOING_CALLS)它必须主动注册为设备上的默认电话或助理处理程序。

在这种情况下,只有当您想读取 OutCommingCall 号码时,您才有 2 个解决方案:

将声明表发送至 google declaration form

或者制作您的应用程序拨号器应用程序

检查Developer Policy Center

关于java - 如何在 Android Q 中检测拨出电话号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56900946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com