简体   繁体   English

用户离线时如何使用自定义的“无Internet连接可用”布局?

[英]How to use a custom No Internet Connection Available layout when user is offline?

I have been trying to use a custom Image that is to be shown to a user when he is offline and when the user clicks on the image, the activity should be reloaded. 我一直在尝试使用自定义图像,该图像将在用户脱机时显示给用户,并且当用户单击图像时,应该重新加载活动。

Ps I am using Blogger API ps我正在使用Blogger API

first add this permission to manifest 首先添加此权限以清单

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

thin in your activty 活动薄弱

private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager 
          = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

and use like this 像这样使用

if(isNetworkAvailable()){
//internt connect
}else{
// no network 
//you can show image here by adding layout and set visibility gone and when no connection set visible
}

You can use Custom Dialog of full Screen to check for internet. 您可以使用全屏的自定义对话框来检查互联网。 So, you can write, 所以,你可以写

if(isNetworkAvailable()){
        //Your Logic for Internet Connection
 }else {
    val noInternetDialog = Dialog(this, android.R.style.Theme)
    noInternetDialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
    noInternetDialog.setContentView(R.layout.no_internet_layout)
    val window = noInternetDialog.window
    window.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)
    // What ever you have widget, can use them here.
    //for example
    val button_try_again = noInternetDialog.findViewById(R.id.buttonId)
    val image_to_display = noInternetDialog.findViewById(R.id.imageId)
    // listeners for image

    noInternetDialog.show
 }

isNetworkAvaibale() is, isNetworkAvaibale()是,

fun isNetworkAvailable(): Boolean {

        val connectivityManager = ApplicationInit.getAppContext()
                .getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val activeNetworkInfo = connectivityManager.activeNetworkInfo
        return activeNetworkInfo != null && activeNetworkInfo.isConnected
    } 

PS : Do not forget to add Internet Permissions PS:不要忘了添加Internet权限

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 互联网连接可用时正在加载离线页面 - Offline Page is getting loaded when internet connection is available 没有使用Firebase的互联网连接时如何获取对手用户的在线/离线状态 - How to get online/offline state of the opponent user when there is no internet connection using firebase 互联网离线时如何维护Channel API连接 - How to maintain Channel API connection when internet is offline 当 Android Studio API 30 中没有互联网连接时,将用户带到离线活动屏幕 - Take user to offline activity screen when no internet connection in Android Studio API 30 无法连接互联网时应用崩溃 - App crashing when internet connection is not available 如何确定,如果互联网连接当前可用且在Android设备上有效? - how to determine, if an internet connection is currently available and active on an android device? 如何查看您的网络应用程序是否无法连接互联网 - How to find out if the internet connection is not available from your web app HikariPool-1 - 使用线程时连接不可用 - HikariPool-1 - Connection is not available when use Threading JavaFX:如何使我的自定义组件使用父布局中的所有可用空间? - JavaFX: How to make my custom component use all available space from parent layout? 不存在Internet连接时如何检测套接字异常 - How to detect socket Exception when internet connection is not there
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM