Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 正在从广播接收器关闭正在运行的应用程序_Android_Android Intent_Android Activity_Broadcastreceiver - Fatal编程技术网

Android 正在从广播接收器关闭正在运行的应用程序

Android 正在从广播接收器关闭正在运行的应用程序,android,android-intent,android-activity,broadcastreceiver,Android,Android Intent,Android Activity,Broadcastreceiver,在我的应用程序中,有一个每天检查的条件,如果它为真,那么我希望我的应用程序在运行之间像崩溃一样接近,堆栈也变得清晰 我尝试并测试了许多解决方案,但没有找到一个符合我要求的解决方案。 我的广播接收器: public void onReceive(Context context, Intent intent) { PreferenceForApp prefs = new PreferenceForApp(context); Bundle bundle = inten

在我的应用程序中,有一个每天检查的条件,如果它为真,那么我希望我的应用程序在运行之间像崩溃一样接近,堆栈也变得清晰

我尝试并测试了许多解决方案,但没有找到一个符合我要求的解决方案。 我的广播接收器

 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);
                }
            }
        }
    }
}
和我从broadcastReceiver调用的活动中要完成的代码:

if (getIntent().getBooleanExtra("close_activity",false)) {
    Log.i("Time", "ExitCSPLogin");
    this.finish();
}

此代码不会在运行期间关闭应用程序。

您需要在活动中注册
BroadcastReceiver
,并在要关闭应用程序时将广播发送到
BroadcastReceiver

在您的活动中,尝试以下方法:

IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.package.ACTION_CLOSE");;
BroadcastReceiver Receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            finish();
        }
    };
registerReceiver(Receiver, intentFilter);
在活动的
onDestroy()
方法中取消注册
BroadcastReceiver

@Override
protected void onDestroy() {
    unregisterReceiver(Receiver);
    super.onDestroy();
}
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("com.package.ACTION_CLOSE");
sendBroadcast(broadcastIntent);
现在,当您想要关闭应用程序时,请将广播发送到
广播接收器

@Override
protected void onDestroy() {
    unregisterReceiver(Receiver);
    super.onDestroy();
}
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("com.package.ACTION_CLOSE");
sendBroadcast(broadcastIntent);

希望这有帮助

每次用户进入应用程序时,您必须在应用程序的主活动的
onCreate
方法中检查以下条件。或者在
onResume
中,如果您想立即关闭应用程序

if (!prefs.getIsDeviceValidated()) {
    Log.i("Time", "ExitCSPLogin");
    this.finish();
}
我假设你的应用程序中有不止一个活动,所以我们将把它放在主活动中,而不是在每个活动中检查上面的标志。允许用户使用您的应用程序,直到他/她来到mainActivity


注意:为您的应用程序(加载项清单)创建广播接收器,而不是为特定活动创建广播接收器

每次用户进入您的应用程序时,您都必须在onCreate中使用此标志来检查设备是否有效(它在您的第一个活动中的意思)。试试看。不,我已经按照你的建议修改了代码,但它对我不起作用。它没有出现在主要活动中,因此没有关闭我的应用程序。只需检查我编辑的代码,更改条件,如-->if(!prefs.getIsDeviceValidated())嘿,这对我也不起作用。它不调用MainActivity类。我不知道你为什么在你的接收器中称这两种方法??设置false标志-->prefs.setIsDeviceValidated(false);prefs.setIsLogIn(false);是的,正如我想的,若广播中的条件变为真(每天检查一次这个条件),那个么用户从应用程序或正在运行的应用程序中出来(比如崩溃),若用户再次打开它,必须重新登录。我应该在MainActivity中添加abv 2代码还是从BroadcastReceiver中添加我要调用的活动?@young_08您应该在活动中添加上述2代码以关闭应用程序,并在任何位置发送广播以关闭应用程序