3 回答

TA貢獻(xiàn)1801條經(jīng)驗(yàn) 獲得超16個(gè)贊
您需要添加 PROCESS_OUTGOING_CALLS 權(quán)限
創(chuàng)建 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 文件中添加讀取 outcoming 調(diào)用所需的權(quán)限
<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" />
在運(yùn)行時(shí)請(qǐng)求權(quán)限
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>
此代碼適用于您,但是當(dāng)您需要在 Google play 上上傳您的應(yīng)用程序時(shí),可以使用 NEW_OUTGOING_CALL 和 READ_PHONE_STATE 權(quán)限,但是,您會(huì)收到來(lái)自 playStore 的政策通知,如下所示:
您的應(yīng)用程序清單請(qǐng)求呼叫日志權(quán)限組(例如 PROCESS_OUTGOING_CALLS)它必須主動(dòng)注冊(cè)為設(shè)備上的默認(rèn)電話或助理處理程序。
在這種情況下,只有當(dāng)您想讀取 OutCommingCall Number 時(shí),您才有 2 個(gè)解決方案:
將申報(bào)表發(fā)送到谷歌申報(bào)表
或者制作您的應(yīng)用程序撥號(hào)器應(yīng)用程序

TA貢獻(xiàn)1842條經(jīng)驗(yàn) 獲得超13個(gè)贊
用 Kotlin 而非 Java 回答:
從 sdk >=29(Android 10 及更高版本)開(kāi)始,您可以將您的應(yīng)用程序注冊(cè)為 CallRedirectionService , “以便在 Telecom 與其實(shí)現(xiàn)者之間進(jìn)行交互,以使用可選的重定向/取消目的進(jìn)行撥出呼叫。”
這消除了創(chuàng)建自定義BroadcastReceiver的需要。
1. 在您的 AndroidManifest.xml 文件中:
<service
android:name=".MyCallRedirectionService"
android:exported="true"
android:permission="android.permission.BIND_CALL_REDIRECTION_SERVICE">
<intent-filter>
<action android:name="android.telecom.CallRedirectionService" />
</intent-filter>
</service>
2. 創(chuàng)建MyCallRedirectionService:
class MyCallRedirectionService : CallRedirectionService() {
override fun onPlaceCall(
handle: Uri,
initialPhoneAccount: PhoneAccountHandle,
allowInteractiveResponse: Boolean
) {
// We can get the outgoing number from the handle parameter:
Log.i("Phone Number:", handle.toString())
}
}
3. 使用RoleManager類提示用戶選擇您的應(yīng)用作為他們的 CallRedirectionService:
在這種情況下,我會(huì)在創(chuàng)建應(yīng)用程序后立即請(qǐng)求方法MainActivity onCreate():
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if (!isRedirection())
roleAcquire(RoleManager.ROLE_CALL_REDIRECTION)
}
以下是使用的函數(shù):
private fun isRedirection(): Boolean {
return isRoleHeldByApp(RoleManager.ROLE_CALL_REDIRECTION)
}
private fun isRoleHeldByApp(roleName: String): Boolean {
val roleManager: RoleManager? = getSystemService(RoleManager::class.java)
return roleManager!!.isRoleHeld(roleName)
}
private fun roleAcquire(roleName: String) {
val roleManager: RoleManager?
if (roleAvailable(roleName)) {
roleManager = getSystemService(RoleManager::class.java)
val intent = roleManager.createRequestRoleIntent(roleName)
startActivityForResult(intent, 1)
} else {
Toast.makeText(
this,
"Redirection call with role in not available",
Toast.LENGTH_SHORT
).show()
}
}
private fun roleAvailable(roleName: String): Boolean {
val roleManager: RoleManager? = getSystemService(RoleManager::class.java)
return roleManager!!.isRoleAvailable(roleName)
}

TA貢獻(xiàn)1798條經(jīng)驗(yàn) 獲得超7個(gè)贊
來(lái)自android.intent.action.NEW_OUTGOING_CALL的文檔:
此常量在 API 級(jí)別 29 中已棄用。重定向傳出呼叫的應(yīng)用程序應(yīng)使用 CallRedirectionService API。執(zhí)行呼叫篩選的應(yīng)用程序應(yīng)使用 CallScreeningService API。
https://developer.android.com/reference/android/content/Intent
所以我會(huì)先實(shí)現(xiàn)這個(gè) API 并檢查它是否按預(yù)期工作。
添加回答
舉報(bào)