Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android 10 default sms app dialog not showing up

I've successfully modified my app to be used as default sms app. The problem is with the dialog that should prompt user to set my app as default. It seems to work on older versions, but not on android 10 emulator.

My code:

fun Activity.askToBeDefaultSMSApp() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        val intent = Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT)
        intent.putExtra(
            Telephony.Sms.Intents.EXTRA_PACKAGE_NAME,
            packageName
        )
        startActivityForResult(intent, DEFAULT_SMS_APP_PERMISSION_REQUEST)
    }
}

I do get onActivityResult response which says that permission is granted, yet if I call:

Telephony.Sms.getDefaultSmsPackage(this)

I still see google sms app as default.

What could be wrong?

like image 549
SMGhost Avatar asked Jan 01 '20 18:01

SMGhost


1 Answers

For android 10 we use RollManger

if (getDefaultSmsPackage(mContext) != null && !getDefaultSmsPackage(mContext).equals(mContext.getPackageName())) {
    RoleManager roleManager = null;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        roleManager = mContext.getSystemService(RoleManager.class);

        if (roleManager.isRoleAvailable(RoleManager.ROLE_SMS)) {
            if (roleManager.isRoleHeld(RoleManager.ROLE_SMS)) {
                Log.d("role", "role");
            } else {
                Intent roleRequestIntent = roleManager . createRequestRoleIntent (
                        RoleManager.ROLE_SMS);
                ((Activity) mContext).startActivityForResult(roleRequestIntent, MESSAGE_CODE);
            }
        }
    } else {
        Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
        intent.putExtra(
            Telephony.Sms.Intents.EXTRA_PACKAGE_NAME,
            mContext.getPackageName()
        );
        ((Activity) mContext).startActivityForResult(intent, MESSAGE_CODE);
    }
}
like image 86
nitinkumarveridic Avatar answered Oct 02 '22 20:10

nitinkumarveridic