- public class NetUtil {
- /**
- * 检查当前的网络
- */
- public static boolean checkNet(Context context) {
- // 判断当前用户手机是否使用WIFI通信
- boolean isWIFI = isWIFIConnection(context);
- // 判断当前用户手机是否使用APN通信
- boolean isAPN = isAPNConnection(context);
- // 否——提示用户——界面跳转到网络设置
- if (isAPN == false && isWIFI == false) {
- return false;
- }
- if (isAPN) {
- // 是接入点
- // IP是10.0.0.172 端口是80 从手机的配置信息中读取
- // 有一部分手机:010.000.000.172
- setAPN(context);
- }
- return true;
- }
- /**
- * 从手机的配置信息中读取
- */
- private static void setAPN(Context context) {
- Uri PREFERRED_APN_URI = Uri.parse("content://telephony/carriers/preferapn");
- ContentResolver contentResolver = context.getContentResolver();
- Cursor query = contentResolver.query(PREFERRED_APN_URI, null, null, null, null);//获取到当前正在连接的APN信息
- if(query!=null&&query.moveToNext())
- {
- //设置ip和端口的信息
- GlobalParams.IP=query.getString(query.getColumnIndex("proxy"));
- GlobalParams.PORT=query.getInt(query.getColumnIndex("port"));
- }
- }
- /**
- * 判断WIFI是否连接
- *
- * @return
- */
- private static boolean isWIFIConnection(Context context) {
- ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo networkInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
- if (networkInfo != null)
- return networkInfo.isConnected();
- return false;
- }
- /**
- * 判断APN是否连接
- *
- * @return
- */
- private static boolean isAPNConnection(Context context) {
- ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo networkInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
- if (networkInfo != null)
- return networkInfo.isConnected();
- return false;
- }
- }
来源: http://www.phpxs.com/code/1002766/