如何在没有Internet连接时显示自定义对话框?

Elj*_*jas 2 android android-layout network-connection

我希望在没有连接到Internet时在webview应用程序中显示自定义对话框.如何检查Internet连接,然后调用对话框?

Phi*_*hil 6

您可以使用ConnectivityManager检查是否存在Internet连接,并且可以向用户显示Toast AlertDialog消息.

另请参见:AlertDialog.Builder

编辑: 以下是如何使用Toast消息执行此操作的示例:

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();
}
Run Code Online (Sandbox Code Playgroud)