Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TelecomManager.ACTION_CHANGE_DEFAULT_DIALER returns RESULT_CANCELED on huawei P8 Lite

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.

like image 728
Yousaf Bungash Avatar asked Dec 11 '22 03:12

Yousaf Bungash


2 Answers

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.

like image 188
Tushar Monirul Avatar answered Jan 11 '23 23:01

Tushar Monirul


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.

like image 44
Pygmalion Avatar answered Jan 12 '23 00:01

Pygmalion