1

Here I want to show two dialog boxes...one for if there is net connection available and other if there is no connection..but i want that when one dialog box is shown, the other dialogue box should be dismissed .......dismiss() is not working in this case....and somehow if I use AlertDialog instead of AlertDialog.Builder to use dismiss(), then i am not able give setPositive, setNegative and setNeutral buttons....any help will be appreciated.......

BroadcastReceiver br;

@Override
protected void onCreate(Bundle savedInstanceState) {
   ...........//

    getStarted();
}
private void getStarted() {

    if (br == null) {

        br = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {

                ...............//

                if (state == NetworkInfo.State.CONNECTED) {

                    AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
                    builder1.setCancelable(false);
                    builder1.setTitle("Connected");
                    builder1.setMessage("Online");


                    builder1.setNeutralButton("Exit", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                           //
                        }
                    });

                    builder1.show();

                }

                else {

                    AlertDialog.Builder builder = new AlertDialog.Builder(context);
                    builder.setCancelable(false);
                    builder.setTitle("No Internet ");
                    builder.setMessage("Offline");


                    builder.setNeutralButton("Exit", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                    //
                        }
                    });

                    builder.show();
                }

            }

        };

        final IntentFilter if = new IntentFilter();
        if.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        getActivity().registerReceiver(br, if);
    }
}
}
Alex
  • 41
  • 1
  • 9
  • Above code is working fine for checking internet status...my question here is to dismiss dialog box on opening of another dialog box..... – Alex Jul 22 '17 at 12:54

2 Answers2

1

Dismiss Your dialog if NetworkInfo.State.CONNECTED is connected,Please change builder1.show(); into builder1.dismiss();

  if (state == NetworkInfo.State.CONNECTED) {

                        AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
                        builder1.setCancelable(false);
                        builder1.setTitle("Connected");
                        builder1.setMessage("Online");


                        builder1.setNeutralButton("Exit", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                               //
                            }
                        });

                        builder1.dismiss();

                    }
piet.t
  • 11,718
  • 21
  • 43
  • 52
Gowthaman M
  • 8,057
  • 8
  • 35
  • 54
  • dismiss() doesn't work in AlertDialog.Builder....I have already mentioned when i was posting the problem i am facing..... – Alex Jul 22 '17 at 13:15
0

Use broadcast receiver to react when the connection is changed with intent filter android.net.ConnectivityManager.CONNECTIVITY_ACTION. So, you can do your stuffs when the receiver receive the intent (or there connection is changed). See here.

Steve Lukis
  • 420
  • 4
  • 10