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

标签 android android-layout network-connection

我想在没有连接到互联网时在 webview 应用程序中显示自定义对话框。如何检查互联网连接,然后调用对话框?

最佳答案

您可以使用ConnectivityManager检查是否有互联网连接,您可以显示 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();
}

关于android - 当没有 Internet 连接时如何显示自定义对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6905262/

相关文章:

android - Android日历标准按钮中的加号和减号按钮吗?

android - Android Fragments 中方向改变时的布局改变

android - 如何防止 ListView 项目以黑色突出显示

c# - 插入网线/连接 Wifi 时收到通知

java - 如何访问 Android Studio 中不同类中的 ImageButton?

java - 无法以编程方式在电子邮件中附加图片

ios - 如何测试支持 IPv6 的 iOS 应用程序? Apple 拒绝了应用程序,因为它不兼容 IPv6

ios - 在 iOS 7 for iPhone SDK 中检查网络可用性

android - Loopj(AsyncHttpClient get 方法)在 Android 单元测试中不返回响应

android - CoordinatorLayout 推送的 Actionbar