Reputation: 1207
I want to show a custom dialog in a webview app when there is no connection to the Internet. How can I check the Internet connection and after that call a dialog?
Upvotes: 2
Views: 2884
Reputation: 36289
You can use ConnectivityManager to check if there is an internet connection, and you can show a Toast AlertDialog message to the user.
See also: AlertDialog.Builder
Edit: Here is an example of how to do this with a Toast message:
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
if (info != null) {
if (!info.isConnected()) {
Toast.makeText(this, "Please check your wireless connection and try again.", Toast.LENGTH_SHORT).show();
}
}
else {
Toast.makeText(this, "Please check your wireless connection and try again.", Toast.LENGTH_SHORT).show();
}
Upvotes: 6
Reputation: 5076
As Phil mentioned, the ConnectivityManager is the way to detect the internet connection on n Android app. However, if you don't want to use that and instead you want to have your app entirely in HTML, you could always show a locally available HTML page on your application. This local HTML could try to check if there is a connection to your server, and, if so, direct the user to your online page. If there is no connection, the local HTML page can show useful offline data, or just a message saying "Sorry, no internet connection."
Upvotes: 0