轉接來電
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
搭載 Android 10 以上版本的裝置管理呼叫意圖的方式與管理方式有所不同
搭載 Android 9 以下版本的裝置。在 Android 10 以上版本中,
ACTION_NEW_OUTGOING_CALL
敬上
應以
CallRedirectionService
。
也能使用 Google Cloud CLI 或
Compute Engine APICallRedirectionService
提供的介面可用於
修改 Android 平台撥出的電話。例如,第三方
應用程式可能會取消通話,並透過 IP 網路語音傳遞技術重新轉送。
Kotlin
class RedirectionService : CallRedirectionService() {
override fun onPlaceCall(
handle: Uri,
initialPhoneAccount: PhoneAccountHandle,
allowInteractiveResponse: Boolean
) {
// Determine if the call should proceed, be redirected, or cancelled.
val callShouldProceed = true
val callShouldRedirect = false
when {
callShouldProceed -> {
placeCallUnmodified()
}
callShouldRedirect -> {
// Update the URI to point to a different phone number or modify the
// PhoneAccountHandle and redirect.
redirectCall(handle, initialPhoneAccount, true)
}
else -> {
cancelCall()
}
}
}
}
Java
class RedirectionService extends CallRedirectionService {
@Override
public void onPlaceCall(
@NonNull Uri handle,
@NonNull PhoneAccountHandle initialPhoneAccount,
boolean allowInteractiveResponse
) {
// Determine if the call should proceed, be redirected, or cancelled.
// Your app should implement this logic to determine the redirection.
boolean callShouldProceed = true;
boolean callShouldRedirect = false;
if (callShouldProceed) {
placeCallUnmodified();
} else if (callShouldRedirect) {
// Update the URI to point to a different phone number or modify the
// PhoneAccountHandle and redirect.
redirectCall(handle, initialPhoneAccount, true);
} else {
cancelCall();
}
}
}
您必須在資訊清單中註冊這項服務,系統才能啟動服務
正確。
<service
android:name=".RedirectionService"
android:permission="android.permission.BIND_CALL_REDIRECTION_SERVICE">
<intent-filter>
<action android:name="android.telecom.CallRedirectionService"/>
</intent-filter>
</service>
如要使用重新導向服務,應用程式必須要求來電轉接角色
來自 RoleManager
。這麼做會要求
使用者,如果想允許應用程式處理呼叫重新導向。如果您的應用程式
未獲授予這個角色,系統就不會使用重新導向服務。
您應該檢查使用者啟動應用程式時,應用程式是否具備上述角色,以便
但你可視需求提出申請啟動由 RoleManager
建立的意圖時,
因此,請務必覆寫
onActivityResult()
處理使用者選擇的函式。
Kotlin
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Tell the system that you want your app to handle call redirects. This
// is done by using the RoleManager to register your app to handle redirects.
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
val roleManager = getSystemService(Context.ROLE_SERVICE) as RoleManager
// Check if the app needs to register call redirection role.
val shouldRequestRole = roleManager.isRoleAvailable(RoleManager.ROLE_CALL_REDIRECTION) &&
!roleManager.isRoleHeld(RoleManager.ROLE_CALL_REDIRECTION)
if (shouldRequestRole) {
val intent = roleManager.createRequestRoleIntent(RoleManager.ROLE_CALL_REDIRECTION)
startActivityForResult(intent, REDIRECT_ROLE_REQUEST_CODE)
}
}
}
companion object {
private const val REDIRECT_ROLE_REQUEST_CODE = 1
}
}
Java
class MainActivity extends AppCompatActivity {
private static final int REDIRECT_ROLE_REQUEST_CODE = 0;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Tell the system that you want your app to handle call redirects. This
// is done by using the RoleManager to register your app to handle redirects.
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
RoleManager roleManager = (RoleManager) getSystemService(Context.ROLE_SERVICE);
// Check if the app needs to register call redirection role.
boolean shouldRequestRole = roleManager.isRoleAvailable(RoleManager.ROLE_CALL_REDIRECTION) &&
!roleManager.isRoleHeld(RoleManager.ROLE_CALL_REDIRECTION);
if (shouldRequestRole) {
Intent intent = roleManager.createRequestRoleIntent(RoleManager.ROLE_CALL_REDIRECTION);
startActivityForResult(intent, REDIRECT_ROLE_REQUEST_CODE);
}
}
}
}
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Redirect a call\n\nDevices that run Android 10 or higher manage call intents differently than\ndevices that run Android 9 or lower. On Android 10 and higher, the\n[`ACTION_NEW_OUTGOING_CALL`](/reference/android/content/Intent#ACTION_NEW_OUTGOING_CALL)\nbroadcast is deprecated and replaced with the\n[`CallRedirectionService`](/reference/android/telecom/CallRedirectionService)\nAPI. The `CallRedirectionService` provides interfaces for you to use to\nmodify outgoing calls made by the Android platform. For example, third-party\napps might cancel calls and reroute them over VoIP. \n\n### Kotlin\n\n```kotlin\nclass RedirectionService : CallRedirectionService() {\n override fun onPlaceCall(\n handle: Uri,\n initialPhoneAccount: PhoneAccountHandle,\n allowInteractiveResponse: Boolean\n ) {\n // Determine if the call should proceed, be redirected, or cancelled.\n val callShouldProceed = true\n val callShouldRedirect = false\n when {\n callShouldProceed -\u003e {\n placeCallUnmodified()\n }\n callShouldRedirect -\u003e {\n // Update the URI to point to a different phone number or modify the\n // PhoneAccountHandle and redirect.\n redirectCall(handle, initialPhoneAccount, true)\n }\n else -\u003e {\n cancelCall()\n }\n }\n }\n}\n```\n\n### Java\n\n```java\nclass RedirectionService extends CallRedirectionService {\n @Override\n public void onPlaceCall(\n @NonNull Uri handle,\n @NonNull PhoneAccountHandle initialPhoneAccount,\n boolean allowInteractiveResponse\n ) {\n // Determine if the call should proceed, be redirected, or cancelled.\n // Your app should implement this logic to determine the redirection.\n boolean callShouldProceed = true;\n boolean callShouldRedirect = false;\n if (callShouldProceed) {\n placeCallUnmodified();\n } else if (callShouldRedirect) {\n // Update the URI to point to a different phone number or modify the\n // PhoneAccountHandle and redirect.\n redirectCall(handle, initialPhoneAccount, true);\n } else {\n cancelCall();\n }\n }\n}\n```\n\nYou must register this service in the manifest so the system can start it\ncorrectly. \n\n \u003cservice\n android:name=\".RedirectionService\"\n android:permission=\"android.permission.BIND_CALL_REDIRECTION_SERVICE\"\u003e\n \u003cintent-filter\u003e\n \u003caction android:name=\"android.telecom.CallRedirectionService\"/\u003e\n \u003c/intent-filter\u003e\n \u003c/service\u003e\n\nTo use a redirection service, your app must request the call redirection role\nfrom the [`RoleManager`](/reference/android/app/role/RoleManager). This will ask\nthe user if they want to allow your app to handle call redirects. If your app\nisn't granted this role, your redirection service isn't used.\n\nYou should check if your app has this role when the user launches your app so\nyou can request it as needed. You launch an intent created by the `RoleManager`,\nso ensure you override the\n[`onActivityResult()`](/reference/android/app/Activity#onActivityResult(int,%20int,%20android.content.Intent))\nfunction to handle the user's selection. \n\n### Kotlin\n\n```kotlin\nclass MainActivity : AppCompatActivity() {\n override fun onCreate(savedInstanceState: Bundle?) {\n super.onCreate(savedInstanceState)\n setContentView(R.layout.activity_main)\n\n // Tell the system that you want your app to handle call redirects. This\n // is done by using the RoleManager to register your app to handle redirects.\n if (android.os.Build.VERSION.SDK_INT \u003e= android.os.Build.VERSION_CODES.Q) {\n val roleManager = getSystemService(Context.ROLE_SERVICE) as RoleManager\n // Check if the app needs to register call redirection role.\n val shouldRequestRole = roleManager.isRoleAvailable(RoleManager.ROLE_CALL_REDIRECTION) &&\n !roleManager.isRoleHeld(RoleManager.ROLE_CALL_REDIRECTION)\n if (shouldRequestRole) {\n val intent = roleManager.createRequestRoleIntent(RoleManager.ROLE_CALL_REDIRECTION)\n startActivityForResult(intent, REDIRECT_ROLE_REQUEST_CODE)\n }\n }\n }\n\n companion object {\n private const val REDIRECT_ROLE_REQUEST_CODE = 1\n }\n}\n```\n\n### Java\n\n```java\nclass MainActivity extends AppCompatActivity {\n private static final int REDIRECT_ROLE_REQUEST_CODE = 0;\n\n @Override\n protected void onCreate(@Nullable Bundle savedInstanceState) {\n super.onCreate(savedInstanceState);\n setContentView(R.layout.activity_main);\n\n // Tell the system that you want your app to handle call redirects. This\n // is done by using the RoleManager to register your app to handle redirects.\n if (android.os.Build.VERSION.SDK_INT \u003e= android.os.Build.VERSION_CODES.Q) {\n RoleManager roleManager = (RoleManager) getSystemService(Context.ROLE_SERVICE);\n // Check if the app needs to register call redirection role.\n boolean shouldRequestRole = roleManager.isRoleAvailable(RoleManager.ROLE_CALL_REDIRECTION) &&\n !roleManager.isRoleHeld(RoleManager.ROLE_CALL_REDIRECTION);\n if (shouldRequestRole) {\n Intent intent = roleManager.createRequestRoleIntent(RoleManager.ROLE_CALL_REDIRECTION);\n startActivityForResult(intent, REDIRECT_ROLE_REQUEST_CODE);\n }\n }\n }\n}\n```"]]