TelecomManager.ACTION_CHANGE_DEFAULT_DIALER returns RESULT_CANCELED on huawei P8 Lite
Asked Answered
C

4

5

I want to change Android default dialer and want to make my own customized dialer. For this purpose I have choose this GIthub repo as start up project. This works well on all other phone and stops working on huawei p8 lite. The default pop up message does not shows up for setting the app as default. Here is code block

private fun checkDefaultDialer() {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return

    val telecomManager = getSystemService(TELECOM_SERVICE) as TelecomManager
    val isAlreadyDefaultDialer = packageName == telecomManager.defaultDialerPackage
    if (isAlreadyDefaultDialer) return

    val intent = Intent(TelecomManager.ACTION_CHANGE_DEFAULT_DIALER).putExtra(TelecomManager.EXTRA_CHANGE_DEFAULT_DIALER_PACKAGE_NAME, packageName)
    startActivityForResult(intent, REQUEST_CODE_SET_DEFAULT_DIALER)
}

private fun checkSetDefaultDialerResult(resultCode: Int) {
    val message = when (resultCode) {
        RESULT_OK -> "User accepted request to become default dialer"
        RESULT_CANCELED -> "User declined request to become default dialer"
        else -> "Unexpected result code $resultCode"
    }
    Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
}

StertActivityforResult returns with RESULT_CANCELED and message

User declined request to become default dialer

Can't find any solution for this. Any help will be greatly appreciated.

Cassaundracassava answered 10/4, 2019 at 12:17 Comment(0)
C
6

If you run this code in Android Q or above it will not work. Its just fine for below Q. To get it working in Android Q try the code below:

RoleManager rm = (RoleManager) getSystemService(Context.ROLE_SERVICE);
startActivityForResult(rm.createRequestRoleIntent(RoleManager.ROLE_DIALER), 120);

It will popup the app chooser dialog.

Chinook answered 2/10, 2020 at 4:22 Comment(0)
A
4

You'll also get a RESULT_CANCELED when targeting Android Q or higher, as the PermissionPolicyService removes the Action. You should use RoleManager.createRequestRoleIntent() instead.

Antisthenes answered 29/5, 2020 at 12:0 Comment(0)
V
3

Try to add some intent filters to your Activity in AndroidManifest.

<intent-filter>
    <action android:name="android.intent.action.VIEW"/>
    <action android:name="android.intent.action.DIAL"/>

    <category android:name="android.intent.category.DEFAULT"/>
    <category android:name="android.intent.category.BROWSABLE"/>

    <data android:scheme="tel"/>
</intent-filter>
<intent-filter>
    <action android:name="android.intent.action.DIAL"/>

    <category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
Valve answered 16/4, 2019 at 20:33 Comment(0)
M
2

My way:

@SuppressLint("QueryPermissionsNeeded")
@TargetApi(Build.VERSION_CODES.M)
fun launchSetDefaultDialerIntent(activity: AppCompatActivity) {
    Intent(TelecomManager.ACTION_CHANGE_DEFAULT_DIALER).putExtra(
        TelecomManager.EXTRA_CHANGE_DEFAULT_DIALER_PACKAGE_NAME,
        activity.packageName
    ).apply {
        if (resolveActivity(activity.packageManager) != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                val rm: RoleManager? = activity.getSystemService(RoleManager::class.java)
                if (rm?.isRoleAvailable(RoleManager.ROLE_DIALER) == true) {
                    @Suppress("DEPRECATION")
                    activity.startActivityForResult(
                        rm.createRequestRoleIntent(RoleManager.ROLE_DIALER),
                        REQUEST_CODE_SET_DEFAULT_DIALER
                    )
                }
            } else {
                @Suppress("DEPRECATION")
                activity.startActivityForResult(this, REQUEST_CODE_SET_DEFAULT_DIALER)
            }
        } else {
            activity.toastShort(R.string.no_contacts_found)
        }
    }
}

Then use it directly in Activity.

Multiracial answered 14/1, 2021 at 8:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.