How do I show a custom dialog when there is no Internet connection?
I want to show a custom dialog in a webview app when there i开发者_如何学Cs no connection to the Internet. How can I check the Internet connection and after that call a dialog?
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();
}
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."
精彩评论