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