權限:android
<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() {app
public void onClick(
DialogInterface dialoginterface, int i) {less
// 手機系統版本 大於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();
}
}
ui