【发布时间】:2015-09-28 06:28:22
【问题描述】:
在我的应用程序中,有一个条件每天检查,如果它成立,那么我希望我的应用程序在运行之间接近崩溃,并且堆栈也变得清晰。
我尝试并测试了许多解决方案,但没有找到符合我要求的解决方案。 我的BroadcastReceiver:
public void onReceive(Context context, Intent intent) {
PreferenceForApp prefs = new PreferenceForApp(context);
Bundle bundle = intent.getExtras();
if (bundle!=null){
if(bundle.containsKey("exception")) {
// String e = bundle.getString("exception")
if(bundle.get("exception").toString().equalsIgnoreCase("http request failed with error_msg No Match Found")) {
prefs.setIsDeviceValidated(false);
prefs.setIsLogIn(false);
Log.i("Time", "Exception Occur");
Intent CSPIntent=new Intent(context,CSPLoginActivity.class);
CSPIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
CSPIntent.putExtra("close_activity", true);
Log.i("Time", "IntentExit");
context.startActivity(CSPIntent);
}
}
}
}
}
以及在我从广播接收器调用的 Activity 中完成的代码:
if (getIntent().getBooleanExtra("close_activity",false)) {
Log.i("Time", "ExitCSPLogin");
this.finish();
}
此代码不会在运行之间关闭应用程序。
【问题讨论】:
-
你必须在 onCreate 中使用这个标志来检查设备是否有效,每次用户进入你的应用程序(它在你的第一个活动中的意思)。试试看。
标签: android android-intent android-activity broadcastreceiver