[英]How to use a custom No Internet Connection Available layout when user is offline?
我一直在尝试使用自定义图像,该图像将在用户脱机时显示给用户,并且当用户单击图像时,应该重新加载活动。
ps我正在使用Blogger API
首先添加此权限以清单
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
活动薄弱
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
像这样使用
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
}
您可以使用全屏的自定义对话框来检查互联网。 所以,你可以写
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()是,
fun isNetworkAvailable(): Boolean {
val connectivityManager = ApplicationInit.getAppContext()
.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val activeNetworkInfo = connectivityManager.activeNetworkInfo
return activeNetworkInfo != null && activeNetworkInfo.isConnected
}
PS:不要忘了添加Internet权限
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.