Przekierowywanie połączenia
Zadbaj o dobrą organizację dzięki kolekcji
Zapisuj i kategoryzuj treści zgodnie ze swoimi preferencjami.
Urządzenia z Androidem 10 lub nowszym zarządzają intencjami połączeń w inny sposób niż
na urządzeniach z Androidem 9 lub starszym. W Androidzie 10 i nowszych parametr
ACTION_NEW_OUTGOING_CALL
komunikat jest wycofany i zastąpiony przez
CallRedirectionService
API. CallRedirectionService
udostępnia interfejsy, których możesz używać do:
modyfikować połączenia wychodzące wykonywane przez platformę Android. Na przykład, jeśli firma zewnętrzna
aplikacje mogą anulować połączenia i przekierowywać je przez VoIP.
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();
}
}
}
Musisz zarejestrować tę usługę w pliku manifestu, aby system mógł ją uruchomić
.
<service
android:name=".RedirectionService"
android:permission="android.permission.BIND_CALL_REDIRECTION_SERVICE">
<intent-filter>
<action android:name="android.telecom.CallRedirectionService"/>
</intent-filter>
</service>
Aby można było korzystać z usługi przekierowania, aplikacja musi prosić o przyznanie roli przekierowywania połączeń
z RoleManager
. Zostanie wyświetlone pytanie
użytkownika, jeśli chce on zezwolić aplikacji na obsługę przekierowań połączeń. Jeśli Twoja aplikacja
nie ma przypisanej tej roli, usługa przekierowująca nie jest używana.
Sprawdzaj, czy Twoja aplikacja ma tę rolę, kiedy użytkownik ją uruchamia.
możesz o niego poprosić. Uruchamiasz intencję utworzoną przez RoleManager
,
więc koniecznie zastąp
onActivityResult()
.
do obsługi wyboru użytkownika.
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);
}
}
}
}
Treść strony i umieszczone na niej fragmenty kodu podlegają licencjom opisanym w Licencji na treści. Java i OpenJDK są znakami towarowymi lub zastrzeżonymi znakami towarowymi należącymi do firmy Oracle lub jej podmiotów stowarzyszonych.
Ostatnia aktualizacja: 2025-07-27 UTC.
[[["Łatwo zrozumieć","easyToUnderstand","thumb-up"],["Rozwiązało to mój problem","solvedMyProblem","thumb-up"],["Inne","otherUp","thumb-up"]],[["Brak potrzebnych mi informacji","missingTheInformationINeed","thumb-down"],["Zbyt skomplikowane / zbyt wiele czynności do wykonania","tooComplicatedTooManySteps","thumb-down"],["Nieaktualne treści","outOfDate","thumb-down"],["Problem z tłumaczeniem","translationIssue","thumb-down"],["Problem z przykładami/kodem","samplesCodeIssue","thumb-down"],["Inne","otherDown","thumb-down"]],["Ostatnia aktualizacja: 2025-07-27 UTC."],[],[],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```"]]