简体   繁体   中英

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

在此输入图像描述

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.

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

The way you can react on "yes" button click:

private String mDefSmsPackage;

@Override
public void onCreate(@Nullable Bundle state) {
    //...
    mDefSmsPackage = Telephony.Sms.getDefaultSmsPackage(getActivity())
}

@Override
public void onResume() {
    super.onResume();
    String newDefSmsPkg = Telephony.Sms.getDefaultSmsPackage(getActivity());
    if (!TextUtils.equals(mDefSmsPackage, newDefSmsPkg)) {
        mDefSmsPackage = newDefSmsPkg;

        //ON DEF SMS APP CAHNGE...

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM