Alex
Alex

Reputation: 41

I want to dismiss the dialog box as soon as it is connected to internet

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

Upvotes: 1

Views: 295

Answers (2)

Gowthaman M
Gowthaman M

Reputation: 8272

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

                    }

Upvotes: 1

Steve Lukis
Steve Lukis

Reputation: 459

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.

Upvotes: 0

Related Questions