有没有办法检测到“更改默认短信应用程序”对话框显示

时间:2015-05-18 07:21:11

标签: android android-activity smsmanager

我正在恢复KitKat或更高版本的短信,我设法通过以下代码显示“更改默认短信应用”对话框:

Intent sendIntent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
sendIntent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, context.getPackageName());
startActivity(sendIntent);

当然会显示对话框,但是当用户按下手机上的“主页”按钮后,该对话框被取消,因此我想检测是否显示该对话框,并re-show它。

我已经尝试估算顶级跑步任务是 com.android.settings 来做到这一点:

private static boolean isInFront(Context context) {
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
    if (!tasks.isEmpty()) {
        String packageName = tasks.get(0).topActivity.getPackageName();
        return "com.android.settings".equals(packageName);
    }
    return false;
}

它帮助但显然不太准确,当我阅读documentation of that Method时似乎不是一个好方法。

有没有人知道这样做的方法,请提前帮助我,谢谢。

1 个答案:

答案 0 :(得分:0)

是。 1)您可以使用startActivityForResult并检查结果代码。

static final int REQUEST_CODE_SMS_DEFAULT_DIALOG = REQUEST_CODE_FIRST_USER;  // The request code

public void startActivity() {
    Intent sendIntent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
    sendIntent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, context.getPackageName());
    startActivityForResults(sendIntent, REQUEST_CODE_SMS_DEFAULT_DIALOG);
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request we're responding to
    if (requestCode == REQUEST_CODE_SMS_DEFAULT_DIALOG) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            // The user picked a default app.
            // The Intent's data identifies which app was selected.

            // Do something you want here.
        } else {
            // Launch the activity again.
        }
    }
}

2)您可以捕捉结束动作。

@Override
public void onPause() {
    super.onPause();  // Always call the superclass method first

    // Check whatever you want here.
}