标签:
权限:
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
//用于判断和开启网络设置
public class NetCheckWebServer {
public NetCheckWebServer() {
}
public static boolean networkStatus(Context context) {
boolean flag;
NetworkInfo networkinfo;
NetworkInfo networkinfo1;
flag = true;
ConnectivityManager connectivitymanager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
networkinfo = connectivitymanager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
networkinfo1 = connectivitymanager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if ((networkinfo == null || !networkinfo.isConnectedOrConnecting())&& (networkinfo1 == null || !networkinfo1.isConnectedOrConnecting())) {
openNetworkSettings(context);
flag = false;
}
return flag;
}
//跳转到设置界面,手动修改网络状态
private static void openNetworkSettings(final Context context) {
(new android.app.AlertDialog.Builder(context))
.setTitle("未发现网络服务")
.setMessage("是否开启网络服务")
.setPositiveButton("确定",
new android.content.DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialoginterface, int i) {
// 手机系统版本 大于10 就是3.0或以上版本
if (Integer
.parseInt(android.os.Build.VERSION.SDK) > 10) {
Intent intent = new Intent(
"android.settings.WIRELESS_SETTINGS");
context.startActivity(intent);
} else {
Intent intent1 = new Intent("/");
intent1.setComponent(new ComponentName(
"com.android.settings",
"com.android.settings.WirelessSettings"));
intent1.setAction("android.intent.action.VIEW");
context.startActivity(intent1);
dialoginterface.cancel();
}
}
})
.setNegativeButton("取消",
new android.content.DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialoginterface, int i) {
dialoginterface.dismiss();
}
}).show();
}
}
标签:
原文地址:http://my.oschina.net/u/2406195/blog/482373