网络相关的工具类

/**
 * 跟网络相关的工具类
 *   <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 *     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
 */
public class NetUtils {
private NetUtils() {
/* cannot be instantiated */
throw new UnsupportedOperationException("cannot be instantiated");
}

/**
* 判断当前是否有可用的网络以及网络类型 0:无网络 1:WIFI 2:CMWAP 3:CMNET

* @param context
* @return
*/
public static int isNetworkAvailable(Context context) {
ConnectivityManager connectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity == null) {
return 0;
} else {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
NetworkInfo netWorkInfo = info[i];
if (netWorkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
return 1;
} else if (netWorkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
String extraInfo = netWorkInfo.getExtraInfo();
if ("cmwap".equalsIgnoreCase(extraInfo)
|| "cmwap:gsm".equalsIgnoreCase(extraInfo)) {
return 2;
}
return 3;
}
}
}
}
}
return 0;
}
/**
* 判断网络是否连接
*/
public static boolean isConnected(Context context) {
ConnectivityManager connectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);


if (null != connectivity) {


NetworkInfo info = connectivity.getActiveNetworkInfo();
if (null != info && info.isConnected()) {
if (info.getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
return false;
}


/**
* 判断是否是wifi连接
*/
public static boolean isWifi(Context context) {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm == null)
return false;
NetworkInfo activeNetworkInfo = cm.getActiveNetworkInfo();
if (activeNetworkInfo != null) {
return cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI;
} else {
return false;
}
}


/**
* 打开网络设置界面
*/
public static void openSetting(Activity activity) {
Intent intent = new Intent();
if (android.os.Build.VERSION.SDK_INT > 10) {
intent = new Intent(
android.provider.Settings.ACTION_WIRELESS_SETTINGS);
} else {
intent = new Intent();
ComponentName component = new ComponentName("com.android.settings",
"com.android.settings.WirelessSettings");
intent.setComponent(component);
intent.setAction("android.intent.action.VIEW");
}
activity.startActivity(intent);
}


/**
* 判断网络状态,无网可用提示用户进行网络设置
*/
public static boolean getNetWorkStatus(final Activity activity) {
boolean netSataus = false;
ConnectivityManager cwjManager = (ConnectivityManager) activity
.getSystemService(Context.CONNECTIVITY_SERVICE);


if (cwjManager.getActiveNetworkInfo() != null) {
netSataus = cwjManager.getActiveNetworkInfo().isAvailable();
}


if (!netSataus) {
AlertDialog.Builder b = new AlertDialog.Builder(activity).setTitle(
"没有可用的网络").setMessage("是否对网络进行设置?");
b.setPositiveButton("是", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Intent intent = new Intent();
if (android.os.Build.VERSION.SDK_INT > 10) {
intent = new Intent(
android.provider.Settings.ACTION_WIRELESS_SETTINGS);
} else {
intent = new Intent();
ComponentName component = new ComponentName(
"com.android.settings",
"com.android.settings.WirelessSettings");
intent.setComponent(component);
intent.setAction("android.intent.action.VIEW");
}
activity.startActivity(intent);
}
}).setNeutralButton("否", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}
}).show();
}
return netSataus;
}
}

你可能感兴趣的:(android,网络)