Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Get result from change default SMS app dialog

I am working on restoring SMS on KITKAT. Referring to this article I have added the things which are required to set my app as default app for SMS. After adding all required things in manifest file I have write the following code:

if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
{
    mDefaultSmsApp = Telephony.Sms.getDefaultSmsPackage(mContext);
    Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
    intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, mContext.getPackageName());
    mContext.startActivity(intent);
}

enter image description here

The above code shows this dialog but I am unable to get the result from this activity/dialog either user clicked on Yes or No because I want to add listener or get any code which should represent that the user clicked on these buttons. Thanks.

like image 212
NASSER Avatar asked Nov 19 '14 05:11

NASSER


People also ask

How do I change my default SMS app on Android?

Navigate to and open Settings, and then tap Apps. Tap Choose default apps, and then tap SMS app. Select your desired message app.

What is the default Android SMS app?

Open the Android settings and go to the option More..." under "Wireless & Network". Under "Default SMS app" choose mysms to set is as default.

What does set default SMS app mean?

All your existing messages will show up in the new default messaging app when you switch to it. However, you won't be able to use other messaging apps when a different app is set as default. The new SMS will arrive in the currently set default app.


1 Answers

One way to do this is to fire the Intent with startActivityForResult(), and then check the resultCode in the onActivityResult() method. Please note that I've changed the code in the example to run in an Activity's Context.

private static final int DEF_SMS_REQ = 0;
private String mDefaultSmsApp;

...

    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
    {
        mDefaultSmsApp = Telephony.Sms.getDefaultSmsPackage(this);

        if (!getPackageName().equals(mDefaultSmsApp))
        {
            Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
            intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, getPackageName());
            startActivityForResult(intent, DEF_SMS_REQ);
        }
    }       

...

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    switch (requestCode)
    {
        case DEF_SMS_REQ:           
            boolean isDefault = resultCode == Activity.RESULT_OK;
            ...
    }
}

As mentioned in a comment below, apparently checking the result code is not 100% reliable. A safer check is to simply compare your app's package name to the current default in onActivityResult(). There's no need to check the result code at all, like the answer linked in the comment shows.

String currentDefault = Sms.getDefaultSmsPackage(this);
boolean isDefault = getPackageName().equals(currentDefault);
like image 119
Mike M. Avatar answered Sep 20 '22 13:09

Mike M.