简体   繁体   English

关闭广播接收器中的活动

[英]Closing an Activity in Broadcast Receiver

I have a broadcast receiver to check the internet connectivity, in which i have validated if no internet connection I am displaying an error screen through intent.我有一个广播接收器来检查互联网连接,其中我已经验证是否没有互联网连接我通过意图显示错误屏幕。 Now i need to close the error screen when the network is reconnected and display the previous screen from which the network was gone.现在我需要在重新连接网络时关闭错误屏幕并显示网络消失的前一个屏幕。

private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        isConnected = intent.getBooleanExtra(ConnectionDetector.CONNECTED_KEY, false);
        if(isConnected){
            System.out.println("A1");
            finish();
        }
        if (intent.getAction().equals(CONNECTION_BROADCAST_ACTION)) {
            System.out.println("A2");
            No_Network.this.finish();
        }
    }
};

This is my code inside the error screen activity.这是我在错误屏幕活动中的代码。 How to close the activity here?如何在此处关闭活动? finish() is not working.完成()不起作用。

How to close the activity here?如何在此处关闭活动? finish() is not working完成()不工作

It is "not working" because you are not using it correctly.它“不起作用”,因为您没有正确使用它。 The correct approach is to notify the activity and tell it to finish() itself.正确的方法是通知活动并告诉它自己finish() You can do that by sending intent with "magic" code of yours directly to your activity (if it is in singleTop launch mode then startActivity() + onNewIntent() would suffice).您可以通过将带有您的“魔术”代码的意图直接发送到您的活动(如果它处于singleTop启动模式,则startActivity() + onNewIntent()就足够了)。 Alternatively use event bus for communication.或者使用事件总线进行通信。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM