安卓webview未連接網(wǎng)絡(luò)時(shí)提示處理方案代碼!
首先我們需要定義一個(gè)判斷網(wǎng)絡(luò)的公共類NetUtil.java
復(fù)制代碼
import android.content.Context;
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;
}
}
復(fù)制代碼
然后在擁有WebView的Activity的onCreate()方法里調(diào)用這個(gè)公共類,并且做出網(wǎng)絡(luò)異常的判斷
if(!NetUtil.isNetConnected(this)){
//提示用戶網(wǎng)絡(luò)連接異常
}else{
//加載URL
}
至此,簡(jiǎn)單的功能便可實(shí)現(xiàn),維護(hù)到了接口數(shù)據(jù)不被暴漏。