public class CheckNetwork {
// 判断网络设置,如果没有网络,进入网络开启页面
public static void checkNetwork(final Context content) {
if (!isNetWork(content)) {
TextView text = new TextView(content);
text.setText("请开启GPRS或WIFI网络连接");
new AlertDialog.Builder(content)
.setTitle("网络状态提示")
.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
content.startActivity(new Intent(
Settings.ACTION_WIRELESS_SETTINGS));
}
}).create().show();
}
}
// 判断网络是否连接
public static Boolean isNetWork(Context context) {
// 获得网络状态管理器
ConnectivityManager conn = (ConnectivityManager) context
.getSystemService(context.CONNECTIVITY_SERVICE);
if (conn == null) {
return false;
} else {
NetworkInfo netinfo[] = conn.getAllNetworkInfo();
if (netinfo != null) {
for (NetworkInfo net : netinfo) {
if (net.getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}
}