我正在恢复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时似乎不是一个好方法。
有没有人知道这样做的方法,请提前帮助我,谢谢。
答案 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.
}