2

I want my app to become default dialer, but when I'm trying to create dialog for user to set default app, my app is rejecting it itself (it's showing toast "User declined request to become default dialer) Testing on Samsung Galaxy S10+ My MainActivity.kt:

class MainActivity : BaseActivity() {
    companion object {
        private const val REQUEST_CODE_SET_DEFAULT_DIALER = 100
    }
    override val navigationGraphResId = R.navigation.nav_graph

    @RequiresApi(Build.VERSION_CODES.M)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        checkDefaultDialer()
    }

    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)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (requestCode) {
            REQUEST_CODE_SET_DEFAULT_DIALER -> checkSetDefaultDialerResult(resultCode)
        }
    }

    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()
    }

}

I placed intent-filter in AndroidManifest.xml:

 <activity
            android:name=".MainActivity"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <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>

        </activity>

Thanks for help

  • What is your current version of android ? Since Android Q your method is no longer supported. From the offcial docuementation : 'This is no longer supported since Q, please use RoleManager.createRequestRoleIntent(String) with RoleManager.ROLE_DIALER instead' – Arno Aug 06 '20 at 11:49
  • 1
    @Arno thank you so much, i didn't find that it is not supported, thanks a lot! – Aleksander Wardyn Aug 06 '20 at 12:01
  • You're welcome ! Don' forget to upvote my comment if you find it usefull. – Arno Aug 06 '20 at 12:05
  • @Arno i don't see these arrows next to comments, it's so weird, maybe because i am new user? Weird but i appreciate your comment! – Aleksander Wardyn Aug 06 '20 at 12:18

0 Answers0