当用户离线时显示对话框,并在用户上线时将其关闭(使用broadcastReceiver)

时间:2017-09-14 09:35:21

标签: java android dialog broadcastreceiver

这是我的基础活动,它扩展了类activity。我让我的其他活动扩展了这个基类:

public abstract class Base extends Activity {

private BroadcastReceiver netStateReceiver;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(getLayoutResourceId());
}
protected abstract int getLayoutResourceId();

@Override
protected void onPause() {
    if (netStateReceiver != null) {
        unregisterReceiver(netStateReceiver);
        netStateReceiver = null;
    }
    super.onPause();
}

@Override
protected void onResume() {
    if (netStateReceiver == null) {
        netStateReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(final Context context, Intent intent) {

                final Dialog offline = new Dialog(context, android.R.style.Theme_Light);

                //A change occurred in connection state. Check whether user has been become online or offline:
                if (!CheckNet()) {
                    //User became offline (show offline dialog):
                    offline.setContentView(R.layout.activity_offline);
                    offline.setTitle("offline");
                    offline.getWindow().setBackgroundDrawableResource(R.color.transparent);

                    offline.show();

                    final Button retry = (Button) offline.findViewById(R.id.button6);
                    retry.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (CheckNet()) {
                                offline.dismiss();
                            }
                        }
                    });

                }
                else {
                    //User became online (dismiss offline dialog):
                    if (offline.isShowing()) {
                        offline.dismiss();
                    }
                }
            }
        };
        registerReceiver(netStateReceiver, new IntentFilter(Values.CONNECTIVITY_RECEIVER_ACTION));
    }
    super.onResume();
}

private boolean CheckNet() {
    final ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();
    return (activeNetwork != null && activeNetwork.isConnectedOrConnecting());
}
}

正如您在代码中看到的,我已经注册了一个用于检查连接状态的接收器。

我希望当用户离线时,会向用户显示一个对话框并通知他他已离线并应该在线继续。这部分很好用。

我也希望在显示该对话框时,当用户上线时,此对话框将被取消,但此部分不起作用,对话框仍保留在显示屏上。

问题是什么,如何解除对话?

3 个答案:

答案 0 :(得分:1)

您可以在每次广播上创建一个新对话框,而不是使用之前创建的对话框。

使对话框变量成为活动类的成员变量,然后它应该起作用。

private Dialog offline;

@Override
protected void onResume() {
    if (netStateReceiver == null) {
        netStateReceiver = new BroadcastReceiver() {
            @Override
        public void onReceive(final Context context, Intent intent) {

            if (!CheckNet()) {
                if(offline==null || !offline.isShowing()){
                    offline = new Dialog(context, android.R.style.Theme_Light);
                }
                ...
            } else {
                //User became online (dismiss offline dialog):
                if (offline!=null && offline.isShowing()) {
                    offline.dismiss();
                }
            }

答案 1 :(得分:0)

每次收到广播时都会创建一个新的对话框,因此您关闭的对话框与用于显示的对话框完全不同;您目前处于脱机状态"。

尝试将您的"离线"活动中的对话框而不是onReceive回调。

一个简单的例子是:

public abstract class Base extends Activity {

    private BroadcastReceiver netStateReceiver;

    final Dialog offline;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutResourceId());

        // You create the dialog here instead within the onReceive callback
        offline = new Dialog(this, android.R.style.Theme_Light);
        offline.setContentView(R.layout.activity_offline);
        offline.setTitle("offline");
        offline.getWindow().setBackgroundDrawableResource(R.color.transparent);
    }


    @Override
    protected void onResume() {
        if (netStateReceiver == null) {
            netStateReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(final Context context, Intent intent) {
                    if (!CheckNet()) {
                        // Your dialog already exists, just show it immediately
                        offline.show();

                        final Button retry = (Button) offline.findViewById(R.id.button6);
                        retry.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (CheckNet()) {
                                    offline.dismiss();
                                }
                            }
                        });

                    }
                    else {
                        //User became online (dismiss offline dialog):
                        if (offline.isShowing()) {
                            offline.dismiss();
                        }
                    }
                }
            };
            registerReceiver(netStateReceiver, new IntentFilter(Values.CONNECTIVITY_RECEIVER_ACTION));
        }
        super.onResume();
    }
}

答案 2 :(得分:0)

使其可以全局访问对话框对象引用:

{{1}}

然后您就可以关闭对话框了。