Yousaf Bungash
Yousaf Bungash

Reputation: 51

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.

Upvotes: 5

Views: 3319

Answers (4)

javakam
javakam

Reputation: 120

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.

Upvotes: 2

Tushar Monirul
Tushar Monirul

Reputation: 5064

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.

Upvotes: 6

Pygmalion
Pygmalion

Reputation: 571

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.

Upvotes: 4

frezze09
frezze09

Reputation: 96

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>

Upvotes: 3

Related Questions