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

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


我已经使用此意图过滤器在清单中注册了接收器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完全不可能检测到拨出电话号码?


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


慕丝7291255
浏览 188回答 3
3回答

侃侃尔雅

您需要添加 PROCESS_OUTGOING_CALLS 权限创建 OutgoingCallReceiverimport android.app.Service;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.telephony.TelephonyManager;public class OutgoingCallReceiver extends BroadcastReceiver {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onReceive(Context context, Intent intent) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TelephonyManager tm = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (tm.getCallState() == TelephonyManager.CALL_STATE_OFFHOOK) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}在 AndroidManifest 文件中添加读取 outcoming 调用所需的权限&nbsp; <uses-permission android:name="android.permission.NEW_OUTGOING_CALL" />&nbsp; &nbsp; <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />&nbsp; &nbsp; <uses-permission android:name="android.permission.READ_PHONE_STATE" />在运行时请求权限if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; != PackageManager.PERMISSION_GRANTED) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ActivityCompat.requestPermissions(this,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new String[]{Manifest.permission.PROCESS_OUTGOING_CALLS, Manifest.permission.READ_PHONE_STATE},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1);&nbsp; &nbsp; &nbsp; &nbsp; }在 AndroidManifest 文件中添加 OutgoingCallReceiver&nbsp; <receiver&nbsp; &nbsp; &nbsp; &nbsp; android:name=".application.services.OutgoingCallReceiver"&nbsp; &nbsp; &nbsp; &nbsp; android:enabled="true"&nbsp; &nbsp; &nbsp; &nbsp; android:exported="true">&nbsp; &nbsp; &nbsp; &nbsp; <intent-filter>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <action android:name="android.intent.action.NEW_OUTGOING_CALL" />&nbsp; &nbsp; &nbsp; &nbsp; </intent-filter>&nbsp; &nbsp; &nbsp; &nbsp; <intent-filter>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <action android:name="android.intent.action.PHONE_STATE" />&nbsp; &nbsp; &nbsp; &nbsp; </intent-filter>&nbsp; &nbsp; </receiver>此代码适用于您,但是当您需要在 Google play 上上传您的应用程序时,可以使用 NEW_OUTGOING_CALL 和 READ_PHONE_STATE 权限,但是,您会收到来自 playStore 的政策通知,如下所示:您的应用程序清单请求呼叫日志权限组(例如 PROCESS_OUTGOING_CALLS)它必须主动注册为设备上的默认电话或助理处理程序。在这种情况下,只有当您想读取 OutCommingCall Number 时,您才有 2 个解决方案:将申报表发送到谷歌申报表或者制作您的应用程序拨号器应用程序检查开发者政策中心

红颜莎娜

用 Kotlin 而非 Java 回答:从 sdk >=29(Android 10 及更高版本)开始,您可以将您的应用程序注册为 CallRedirectionService , “以便在 Telecom 与其实现者之间进行交互,以使用可选的重定向/取消目的进行拨出呼叫。”这消除了创建自定义BroadcastReceiver的需要。1. 在您的 AndroidManifest.xml 文件中:<service&nbsp; &nbsp; &nbsp; &nbsp;android:name=".MyCallRedirectionService"&nbsp; &nbsp; &nbsp; &nbsp;android:exported="true"&nbsp; &nbsp; &nbsp; &nbsp;android:permission="android.permission.BIND_CALL_REDIRECTION_SERVICE">&nbsp; &nbsp; &nbsp; &nbsp;<intent-filter>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<action android:name="android.telecom.CallRedirectionService" />&nbsp; &nbsp; &nbsp; &nbsp;</intent-filter></service>2. 创建MyCallRedirectionService:class MyCallRedirectionService : CallRedirectionService() {&nbsp; &nbsp; override fun onPlaceCall(&nbsp; &nbsp; &nbsp; &nbsp; handle: Uri,&nbsp; &nbsp; &nbsp; &nbsp; initialPhoneAccount: PhoneAccountHandle,&nbsp; &nbsp; &nbsp; &nbsp; allowInteractiveResponse: Boolean&nbsp; &nbsp; ) {&nbsp; &nbsp; &nbsp; &nbsp; // We can get the outgoing number from the handle parameter:&nbsp; &nbsp; &nbsp; &nbsp; Log.i("Phone Number:", handle.toString())&nbsp; &nbsp; }}3. 使用RoleManager类提示用户选择您的应用作为他们的 CallRedirectionService:在这种情况下,我会在创建应用程序后立即请求方法MainActivity onCreate():override fun onCreate(savedInstanceState: Bundle?) {&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState)&nbsp; &nbsp; &nbsp; &nbsp; setContentView(R.layout.activity_main)&nbsp; &nbsp; &nbsp; &nbsp; if (!isRedirection())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; roleAcquire(RoleManager.ROLE_CALL_REDIRECTION)}以下是使用的函数:&nbsp; &nbsp; private fun isRedirection(): Boolean {&nbsp; &nbsp; &nbsp; &nbsp; return isRoleHeldByApp(RoleManager.ROLE_CALL_REDIRECTION)&nbsp; &nbsp; }&nbsp; &nbsp; private fun isRoleHeldByApp(roleName: String): Boolean {&nbsp; &nbsp; &nbsp; &nbsp; val roleManager: RoleManager? = getSystemService(RoleManager::class.java)&nbsp; &nbsp; &nbsp; &nbsp; return roleManager!!.isRoleHeld(roleName)&nbsp; &nbsp; }&nbsp; &nbsp; private fun roleAcquire(roleName: String) {&nbsp; &nbsp; &nbsp; &nbsp; val roleManager: RoleManager?&nbsp; &nbsp; &nbsp; &nbsp; if (roleAvailable(roleName)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; roleManager = getSystemService(RoleManager::class.java)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val intent = roleManager.createRequestRoleIntent(roleName)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startActivityForResult(intent, 1)&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Redirection call with role in not available",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.LENGTH_SHORT&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ).show()&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; private fun roleAvailable(roleName: String): Boolean {&nbsp; &nbsp; &nbsp; &nbsp; val roleManager: RoleManager? = getSystemService(RoleManager::class.java)&nbsp; &nbsp; &nbsp; &nbsp; return roleManager!!.isRoleAvailable(roleName)&nbsp; &nbsp; }

元芳怎么了

来自android.intent.action.NEW_OUTGOING_CALL的文档:此常量在 API 级别 29 中已弃用。重定向传出呼叫的应用程序应使用 CallRedirectionService API。执行呼叫筛选的应用程序应使用 CallScreeningService API。https://developer.android.com/reference/android/content/Intent所以我会先实现这个 API 并检查它是否按预期工作。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java