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

标签 java android kotlin

我无法在 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/

相关文章:

java - 将 "asdfgh"等字符串转换为整数值

java - Neo4j 3.5 中的事务未实现 java.lang.AutoCloseable

android - ScrollView 中的 View 与配置的父级不匹配

android - Android SDK 的 Windows 和 Linux 版本之间是否存在差异?

applet - 如何在小程序中检测 OpenJDK(系统属性)?

android - 'Android 预编译器' 错误 'Path must include project and resource name'

android - 根据List改变constraintlayout

java - 将 Android Studio 更新到 fox 2020.3.1 后主题插件不起作用

kotlin - 如何使用我的 Kotlin 多平台项目发布 javadoc.jar 文件?

java - cXML 相当于 json 吗?