3 回答

TA貢獻1801條經驗 獲得超16個贊
您需要添加 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 文件中添加讀取 outcoming 調用所需的權限
<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 的政策通知,如下所示:
您的應用程序清單請求呼叫日志權限組(例如 PROCESS_OUTGOING_CALLS)它必須主動注冊為設備上的默認電話或助理處理程序。
在這種情況下,只有當您想讀取 OutCommingCall Number 時,您才有 2 個解決方案:
將申報表發送到谷歌申報表
或者制作您的應用程序撥號器應用程序
檢查開發者政策中心

TA貢獻1842條經驗 獲得超13個贊
用 Kotlin 而非 Java 回答:
從 sdk >=29(Android 10 及更高版本)開始,您可以將您的應用程序注冊為 CallRedirectionService , “以便在 Telecom 與其實現者之間進行交互,以使用可選的重定向/取消目的進行撥出呼叫?!?/p>
這消除了創建自定義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. 創建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類提示用戶選擇您的應用作為他們的 CallRedirectionService:
在這種情況下,我會在創建應用程序后立即請求方法MainActivity onCreate():
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if (!isRedirection())
roleAcquire(RoleManager.ROLE_CALL_REDIRECTION)
}
以下是使用的函數:
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貢獻1798條經驗 獲得超7個贊
來自android.intent.action.NEW_OUTGOING_CALL的文檔:
此常量在 API 級別 29 中已棄用。重定向傳出呼叫的應用程序應使用 CallRedirectionService API。執行呼叫篩選的應用程序應使用 CallScreeningService API。
https://developer.android.com/reference/android/content/Intent
所以我會先實現這個 API 并檢查它是否按預期工作。
添加回答
舉報