【问题标题】:Closing running Application from BroadcastReceiver从 BroadcastReceiver 关闭正在运行的应用程序
【发布时间】: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


【解决方案1】:

您需要在您的活动中注册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()你的Activity方法中注销BroadcastReceiver

@Override
protected void onDestroy() {
    unregisterReceiver(Receiver);
    super.onDestroy();
}

现在当你想关闭应用程序时,发送广播到BroadcastReceiver

Intent broadcastIntent = new Intent();
broadcastIntent.setAction("com.package.ACTION_CLOSE");
sendBroadcast(broadcastIntent);

希望这会有所帮助!

【讨论】:

  • 我应该在 MainActivity 或我想从 BroadcastReceiver 调用的活动中添加 abv 2 代码吗?
  • @young_08 您应该在您的活动中调用以上 2 个代码来关闭应用程序并在任何地方发送广播以关闭应用程序
【解决方案2】:

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

if (!prefs.getIsDeviceValidated()) {
    Log.i("Time", "ExitCSPLogin");
    this.finish();
}

我假设您的应用程序中有多个活动,因此在每个活动中插入检查上述标志,我们将把它放在主要活动中。允许用户使用您的应用,直到他/她来到 mainActivity

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

【讨论】:

  • Nops,我已经按照你的建议修改了代码,但它对我不起作用它没有进入 Main Activity,因此没有关闭我的应用程序。
  • 只需检查我编辑的代码,更改条件如 --> if (!prefs.getIsDeviceValidated())
  • 嘿,这对我也不起作用。它没有调用 MainActivity 类。我不知道为什么
  • 您在接收器中调用这两种方法??设置错误标志 --> prefs.setIsDeviceValidated(false); prefs.setIsLogIn(false);
  • 是的,我希望如果广播中的条件为真(每天检查一次此条件),然后用户从应用程序或正在运行的应用程序中出来(比如崩溃),如果用户再次打开它,必须重新登录。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-05
  • 1970-01-01
  • 2017-05-02
  • 2012-03-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多