使用广播接收器Android关闭应用 [英] Using broadcast receiver android to close app

查看:192
本文介绍了使用广播接收器Android关闭应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图使用​​广播接收器来触发某些动作

Hi am trying to use broadcast receiver to trigger some action


  1. 活动A将广播动作 ACTION_EXIT并进入主要活动。

  1. Activity A will broadcast action "ACTION_EXIT" and come to Main Activity.

只要主活动接收到广播 ACTION_EXIT,它就会关闭该应用。

Whenever Main Activity receive the broadcast "ACTION_EXIT" it will close the app.

我在活动A上的代码(发送广播)

My code on Activity A (to send broadcast)

Intent broadcastIntent = new Intent(Beacon_MainActivity.this, MainActivity.class);
broadcastIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
broadcastIntent.setAction("com.package.ACTION_EXIT");
sendBroadcast(broadcastIntent);
finish();

主要活动代码以接收广播并触发ACTION_EXIT

Code on Main Activity to receive the broadcast and trigger ACTION_EXIT

private void registerReceiver(){
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.package.ACTION_EXIT");
        registerReceiver(myBroadcastReceiver, intentFilter);
    }

@Override
public void onResume() {
    registerReceiver();
    super.onResume();

}

@Override
public void onPause() {
    super.onPause();
    unregisterReceiver(myBroadcastReceiver);
}

BroadcastReceiver myBroadcastReceiver =
        new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                Log.d("onReceive", "Logout in progress");
                Toast.makeText(MainActivity.this, "what the ****", Toast.LENGTH_SHORT).show();
                finish();
            } 
        };

不知道为什么它不起作用,任何人都可以提供帮助

Don't know why it's not working, anyone can help should be much appreciate


(当Main_Activity收到
履历表上的广播时,应用应该关闭)

(app should be close when Main_Activity receive the broadcast on resume)


推荐答案

您的 MainActivity 已暂停,而 ActivityA 已暂停正在运行,在此期间您的接收器未注册,因此无法获得广播。您可以通过结果转发来完成所需的操作。

Your MainActivity is paused while ActivityA is running, during which time your Receiver is unregistered, and therefore not getting the broadcast. You can accomplish what you want with result forwarding instead.

MainActivity 中,启动 LoginActivity startActivityForResult()方法,并覆盖 onActivityResult()方法来处理结果。

In MainActivity, start LoginActivity with the startActivityForResult() method, and override the onActivityResult() method to handle the result.

public static final int REQUEST_CODE = 0;
public static final String EXTRA_EXIT = "exit";
...

Intent actLogin = new Intent(this, LoginActivity.class);
startActivityForResult(actLogin, REQUEST_CODE);
...

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch(requestCode) {
        case REQUEST_CODE:
            if(data == null) {
                return;
            }

            boolean shouldExit = data.getBooleanExtra(EXTRA_EXIT, false);

            if(shouldExit) {
                finish();
            }
            break;
        ...
    }
}

然后,在 LoginActivity ,将 FLAG_ACTIVITY_FORWARD_RESULT 添加到用于启动 ActivityA 的I​​ntent中。

Then, in LoginActivity, add FLAG_ACTIVITY_FORWARD_RESULT to the Intent used to start ActivityA.

Intent actA = new Intent(this, ActivityA.class);
actA.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
startActivity(actA);
finish();

ActivityA 中,设置 boolean 值指示是否退出,将其添加到结果 Intent ,将其设置为结果,然后完成Activity。

In ActivityA, set a boolean value to indicate whether to exit, add it to a result Intent, set that as the result, and finish the Activity.

boolean shouldExit = ...            

Intent result = new Intent();
result.putExtra(MainActivity.EXTRA_EXIT, shouldExit);
setResult(Activity.RESULT_OK, result);
finish();

然后将结果传送回 MainActivity 。这也可以只用结果代码来完成,但是我更喜欢使用Intent Extras。

The result will then be delivered back to MainActivity. This can also be done with only result codes, but I prefer using Intent extras.

这篇关于使用广播接收器Android关闭应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆