Android - Get result from change default SMS app dialog

余生颓废 提交于 2019-12-30 07:04:09

问题


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.


回答1:


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



回答2:


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

    }
}


来源:https://stackoverflow.com/questions/27009971/android-get-result-from-change-default-sms-app-dialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!