if (isConnect(this) == false) {
new AlertDialog.Builder(
this)
.setTitle("网络错误")
.setMessage(
"网络连接失败,
请确认网络连接")
.setPositiveButton(
"确定",
new
DialogInterface.OnClickListener() {
@Override
public
void onClick(
DialogInterface arg0,
int arg1) {
// TODO Auto-generated method stub
}
}).show();
}
private boolean isConnect(
Gongzibai gongzibai) {
ConnectivityManager conManager = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = conManager
.getActiveNetworkInfo();
if (networkInfo != null) { // 注意,这个判断一定要的哦
,要不然会出错
return networkInfo
.isAvailable();
}
return false;
}
--------------manifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />