Android - get result from default SMS application dialog

I am working on SMS recovery on KITKAT. Referring to the article, I added what is needed to install my application as the default application for SMS. After adding all the necessary things to the manifest file, I wrote 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 code above shows this dialog box, but I can’t get the result from this action / dialog box that the user clicked “Yes” or “No” because I want to add a listener or get a code that should represent the user clicked on these buttons. Thank.

+4
source share
2

- Intent startActivityForResult(), resultCode onActivityResult(). , .

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

, , 100% . - onActivityResult(). , , .

String currentDefault = Sms.getDefaultSmsPackage(this);
boolean isDefault = getPackageName().equals(currentDefault);
+9

"":

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

    }
}
0

All Articles