1

I'm trying to see is the user has internet or no. When the user has no internet, it shows a dialog with a message, but if he touches outside of the dialog, the dialog does nothing.

I've tried with:

dialog.setCancelable(false);

dialog.setCanceledOnTouchOutside(false);

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if(!isConnected())
        {
            new AlertDialog.Builder(this)
            .setTitle("No internet")
            .setMessage("Message ")
            .setPositiveButton("Close", new DialogInterface.OnClickListener()
                    {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int w) {
                            finish();
                        }
                    })
.show();
        }
    }
    private boolean isConnected()
    {
        ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

        return networkInfo != null && networkInfo.isConnected();
}

}
Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
GuiSousa
  • 25
  • 6

1 Answers1

2

Use this one:

new AlertDialog.Builder(this)
    .setTitle("No internet")
    .setCancelable(false)
    .setMessage("Message ")
    .setPositiveButton("Close", new DialogInterface.OnClickListener()
    {
      @Override
      public void onClick(DialogInterface dialogInterface, int w) {
        finish();
      }
    })
    .show();
majid ghafouri
  • 787
  • 2
  • 7
  • 23