安卓webview未連接網絡時提示處理方案代碼!
時間:2022年9月28日
瀏覽:次
以下為解決方案代碼:
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class NetUtil {
public static boolean isNetConnected(Context context) {
boolean isNetConnected;
ConnectivityManager connManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = connManager.getActiveNetworkInfo();
if (info != null && info.isAvailable()) {
isNetConnected = true;
} else {
isNetConnected = false;
}
return isNetConnected;
}
}
if(!NetUtil.isNetConnected(this)){
//提示用戶網絡連接異常
}else{
//加載URL
}
至此,簡單的功能便可實現,維護到了接口數據不被暴漏。