简体   繁体   中英

Show dialog when user becomes offline and dismiss it when user becomes online (with broadcastReceiver)

this is my base activity that extends class activity . I make my other activities extend this base class:

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());
}
}

As you see in code I have registered a receiver for checking connectivity status.

I want when user becomes offline a dialog be shown to user and notify him that he is offline and should become online to continue. This part works good.

I also want when that dialog is showing and in the moment user becomes online this dialog be dismissed, but this part doesn't work and dialog stays on the display.

What's the problem, how can I dismiss the dialog?

You create a new dialog on every broadcast instead of using the dialog you already created before.

Make the dialog variable a member variable of the activity class, then it should work.

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();
                }
            }

You are creating a new dialog everytime you receive a broadcast, so the dialog you dismissed is a whole different dialog than the one used to show "you are currently offline".

Try putting your "offline" dialog in the activity instead of within the onReceive callback.

A simple example would be:

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();
    }
}

Make it global access to your dialog object reference :

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

Then you will able to close your dialog.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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