I check for an active connection in onResume()
and if there isn't one I have a method to create an AlertDialog
as follows...
protected void createNetErrorDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("You need a network connection to use this application. Please turn on mobile network or Wi-Fi in Settings.")
.setTitle("Unable to connect")
.setCancelable(false)
.setPositiveButton("Settings",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent i = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
startActivity(i);
}
}
)
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MainActivity.this.finish();
}
}
);
AlertDialog alert = builder.create();
alert.show();
}
If the user hits the Cancel button I finish the Activity
otherwise start Settings. If the user hits BACK in Settings, onResume()
is called again and the network is checked again. This works fine for me.