Java Code Examples for android.telephony.TelephonyManager#isNetworkRoaming()

The following examples show how to use android.telephony.TelephonyManager#isNetworkRoaming() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LogRunnerService.java    From callmeter with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Acquire {@link WakeLock} and init service.
 *
 * @param a action
 */
@SuppressLint({"MissingPermission", "HardwareIds"})
private void acquire(final String a) {
    if (TelephonyManager.ACTION_PHONE_STATE_CHANGED.equals(a) || ACTION_SMS.equals(a)) {
        Log.i(TAG, "sleep for " + WAIT_FOR_LOGS + "ms");
        try {
            Thread.sleep(WAIT_FOR_LOGS);
        } catch (InterruptedException e) {
            Log.e(TAG, "interrupted while waiting for logs", e);
        }
    }

    // update roaming info
    TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    if (tm != null) {
        roaming = tm.isNetworkRoaming();
        Log.d(TAG, "roaming: ", roaming);
        mynumber = tm.getLine1Number();
        Log.d(TAG, "my number: ", mynumber);
    }
}
 
Example 2
Source File: RLNetUtil.java    From Roid-Library with Apache License 2.0 6 votes vote down vote up
/**
 * @param context
 * @return
 */
public static boolean isNetworkRoaming(Context context) {
    ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity == null) {
        Log.w(LOG_TAG, "couldn't get connectivity manager");
    } else {
        NetworkInfo info = connectivity.getActiveNetworkInfo();
        if (info != null && info.getType() == ConnectivityManager.TYPE_MOBILE) {
            TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            if (tm != null && tm.isNetworkRoaming()) {
                Log.d(LOG_TAG, "network is roaming");
                return true;
            } else {
                Log.d(LOG_TAG, "network is not roaming");
            }
        } else {
            Log.d(LOG_TAG, "not using mobile network");
        }
    }
    return false;
}
 
Example 3
Source File: NetWorkUtils.java    From AndroidCacheFoundation with Apache License 2.0 6 votes vote down vote up
/**
 * 判断网络是否为漫游
 */
public static boolean isNetworkRoaming(Context context) {
	ConnectivityManager connectivity = (ConnectivityManager) context
			.getSystemService(Context.CONNECTIVITY_SERVICE);
	if (connectivity == null) {
		Log.w(LOG_TAG, "couldn't get connectivity manager");
	} else {
		NetworkInfo info = connectivity.getActiveNetworkInfo();
		if (info != null
				&& info.getType() == ConnectivityManager.TYPE_MOBILE) {
			TelephonyManager tm = (TelephonyManager) context
					.getSystemService(Context.TELEPHONY_SERVICE);
			if (tm != null && tm.isNetworkRoaming()) {
				Log.d(LOG_TAG, "network is roaming");
				return true;
			} else {
				Log.d(LOG_TAG, "network is not roaming");
			}
		} else {
			Log.d(LOG_TAG, "not using mobile network");
		}
	}
	return false;
}
 
Example 4
Source File: NetWorkHelper.java    From browser with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 判断网络是否为漫�?
 */
public static boolean isNetworkRoaming(Context context) {
    ConnectivityManager connectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity == null) {
        Log.w(LOG_TAG, "couldn't get connectivity manager");
    } else {
        NetworkInfo info = connectivity.getActiveNetworkInfo();
        if (info != null
                && info.getType() == ConnectivityManager.TYPE_MOBILE) {
            TelephonyManager tm = (TelephonyManager) context
                    .getSystemService(Context.TELEPHONY_SERVICE);
            if (tm != null && tm.isNetworkRoaming()) {
                Log.d(LOG_TAG, "network is roaming");
                return true;
            } else {
                Log.d(LOG_TAG, "network is not roaming");
            }
        } else {
            Log.d(LOG_TAG, "not using mobile network");
        }
    }
    return false;
}
 
Example 5
Source File: SystemFacade.java    From UnityOBBDownloader with Apache License 2.0 6 votes vote down vote up
public boolean isNetworkRoaming() {
    ConnectivityManager connectivity =
            (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity == null) {
        Log.w(Constants.TAG, "couldn't get connectivity manager");
        return false;
    }

    NetworkInfo info = connectivity.getActiveNetworkInfo();
    boolean isMobile = (info != null && info.getType() == ConnectivityManager.TYPE_MOBILE);
    TelephonyManager tm = (TelephonyManager) mContext
            .getSystemService(Context.TELEPHONY_SERVICE);
    if (null == tm) {
        Log.w(Constants.TAG, "couldn't get telephony manager");
        return false;
    }
    boolean isRoaming = isMobile && tm.isNetworkRoaming();
    if (Constants.LOGVV && isRoaming) {
        Log.v(Constants.TAG, "network is roaming");
    }
    return isRoaming;
}
 
Example 6
Source File: SystemFacade.java    From play-apk-expansion with Apache License 2.0 6 votes vote down vote up
public boolean isNetworkRoaming() {
    ConnectivityManager connectivity =
            (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity == null) {
        Log.w(Constants.TAG, "couldn't get connectivity manager");
        return false;
    }

    NetworkInfo info = connectivity.getActiveNetworkInfo();
    boolean isMobile = (info != null && info.getType() == ConnectivityManager.TYPE_MOBILE);
    TelephonyManager tm = (TelephonyManager) mContext
            .getSystemService(Context.TELEPHONY_SERVICE);
    if (null == tm) {
        Log.w(Constants.TAG, "couldn't get telephony manager");
        return false;
    }
    boolean isRoaming = isMobile && tm.isNetworkRoaming();
    if (Constants.LOGVV && isRoaming) {
        Log.v(Constants.TAG, "network is roaming");
    }
    return isRoaming;
}
 
Example 7
Source File: NetworkUtil.java    From ONE-Unofficial with Apache License 2.0 6 votes vote down vote up
/**
 * @param context
 * @param
 * @return
 * @description 判断网络是否是漫游
 * @date 2014-12-5
 * @author 史永飞
 */
public boolean isNetworkRoaming(Context context) {
    if (conManager != null) {
        NetworkInfo info = conManager.getActiveNetworkInfo();
        if (info != null
                && info.getType() == ConnectivityManager.TYPE_MOBILE) {
            TelephonyManager tm = (TelephonyManager) context
                    .getSystemService(Context.TELEPHONY_SERVICE);
            if (tm != null && tm.isNetworkRoaming()) {
                Log.d("Tag", "network is roaming");
                return true;
            } else {
                Log.d("Tag", "network is not roaming");
            }
        }
    }
    return false;
}
 
Example 8
Source File: RealSystemFacade.java    From mobile-manager-tool with MIT License 6 votes vote down vote up
public boolean isNetworkRoaming() {
ConnectivityManager connectivity = (ConnectivityManager) mContext
	.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity == null) {
    Log.w(Constants.TAG, "couldn't get connectivity manager");
    return false;
}

NetworkInfo info = connectivity.getActiveNetworkInfo();
boolean isMobile = (info != null && info.getType() == ConnectivityManager.TYPE_MOBILE);
final TelephonyManager mgr = (TelephonyManager) mContext
	.getSystemService(Context.TELEPHONY_SERVICE);
boolean isRoaming = isMobile && mgr.isNetworkRoaming();
if (Constants.LOGVV && isRoaming) {
    Log.v(Constants.TAG, "network is roaming");
}
return isRoaming;
   }
 
Example 9
Source File: SystemFacade.java    From Alite with GNU General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("unused")
public boolean isNetworkRoaming() {
       ConnectivityManager connectivity =
               (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
       if (connectivity == null) {
           Log.w(Constants.TAG, "couldn't get connectivity manager");
           return false;
       }

       NetworkInfo info = connectivity.getActiveNetworkInfo();
       boolean isMobile = (info != null && info.getType() == ConnectivityManager.TYPE_MOBILE);
       TelephonyManager tm = (TelephonyManager) mContext
               .getSystemService(Context.TELEPHONY_SERVICE);
       if (null == tm) {
           Log.w(Constants.TAG, "couldn't get telephony manager");
           return false;
       }
       boolean isRoaming = isMobile && tm.isNetworkRoaming();
       if (Constants.LOGVV && isRoaming) {
           Log.v(Constants.TAG, "network is roaming");
       }
       return isRoaming;
   }
 
Example 10
Source File: SystemFacade.java    From travelguide with Apache License 2.0 6 votes vote down vote up
public boolean isNetworkRoaming() {
    ConnectivityManager connectivity =
            (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity == null) {
        Log.w(Constants.TAG, "couldn't get connectivity manager");
        return false;
    }

    NetworkInfo info = connectivity.getActiveNetworkInfo();
    boolean isMobile = (info != null && info.getType() == ConnectivityManager.TYPE_MOBILE);
    TelephonyManager tm = (TelephonyManager) mContext
            .getSystemService(Context.TELEPHONY_SERVICE);
    if (null == tm) {
        Log.w(Constants.TAG, "couldn't get telephony manager");
        return false;
    }
    boolean isRoaming = isMobile && tm.isNetworkRoaming();
    if (Constants.LOGVV && isRoaming) {
        Log.v(Constants.TAG, "network is roaming");
    }
    return isRoaming;
}
 
Example 11
Source File: NetworkUtils.java    From pandroid with Apache License 2.0 5 votes vote down vote up
/**
 * Get roaming state from the telephony manager
 * (and not current network which could be Wi-Fi and return false every time)
 *
 * @param context
 * @return
 */
public static Boolean isRoaming(Context context) {
    TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    if (telephonyManager != null)
        return telephonyManager.isNetworkRoaming();
    else return false;

}
 
Example 12
Source File: NetworkUtils.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"MissingPermission"})
public static int getAPNType(Context context) {
    int netType = CONNECTION_TYPE_NULL;
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = manager.getActiveNetworkInfo();
    if (networkInfo == null) {
        return CONNECTION_TYPE_NULL;
    }
    int nType = networkInfo.getType();
    if (nType == ConnectivityManager.TYPE_WIFI) {
        netType = CONNECTION_TYPE_WIFI;
    } else if (nType == ConnectivityManager.TYPE_MOBILE) {
        int nSubType = networkInfo.getSubtype();
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (nSubType == TelephonyManager.NETWORK_TYPE_LTE
                && !telephonyManager.isNetworkRoaming()) {
            netType = CONNECTION_TYPE_4G;
        } else if (nSubType == TelephonyManager.NETWORK_TYPE_UMTS
                || nSubType == TelephonyManager.NETWORK_TYPE_HSDPA
                || nSubType == TelephonyManager.NETWORK_TYPE_EVDO_0
                && !telephonyManager.isNetworkRoaming()) {
            netType = CONNECTION_TYPE_3G;
        } else if (nSubType == TelephonyManager.NETWORK_TYPE_GPRS
                || nSubType == TelephonyManager.NETWORK_TYPE_EDGE
                || nSubType == TelephonyManager.NETWORK_TYPE_CDMA
                && !telephonyManager.isNetworkRoaming()) {
            netType = CONNECTION_TYPE_2G;
        } else {
            netType = CONNECTION_TYPE_2G;
        }
    }
    return netType;
}
 
Example 13
Source File: Network.java    From batteryhub with Apache License 2.0 4 votes vote down vote up
public static int getRoamingStatus(Context context) {
    TelephonyManager manager = (TelephonyManager)
            context.getSystemService(Context.TELEPHONY_SERVICE);

    return (manager.isNetworkRoaming()) ? 1 : 0;
}
 
Example 14
Source File: RNSimDataModule.java    From react-native-sim-data with MIT License 4 votes vote down vote up
@Override
public Map<String, Object> getConstants() {

  final Map<String, Object> constants = new HashMap<>();

  try {
    TelephonyManager telManager = (TelephonyManager) this.reactContext.getSystemService(Context.TELEPHONY_SERVICE);

    SubscriptionManager manager = (SubscriptionManager) this.reactContext.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
    List<SubscriptionInfo> subscriptionInfos = manager.getActiveSubscriptionInfoList();

    int sub = 0;
    for (SubscriptionInfo subInfo : subscriptionInfos) {
      CharSequence carrierName = subInfo.getCarrierName();
      String countryIso        = subInfo.getCountryIso();
      int dataRoaming          = subInfo.getDataRoaming();  // 1 is enabled ; 0 is disabled
      CharSequence displayName = subInfo.getDisplayName();
      String iccId             = subInfo.getIccId();
      int mcc                  = subInfo.getMcc();
      int mnc                  = subInfo.getMnc();
      String number            = subInfo.getNumber();
      int simSlotIndex         = subInfo.getSimSlotIndex();
      int subscriptionId       = subInfo.getSubscriptionId();
      boolean networkRoaming   = telManager.isNetworkRoaming();
      String deviceId          = telManager.getDeviceId(simSlotIndex);
      //String deviceId          = telManager.getImei(simSlotIndex) || telManager.getMeid(simSlotIndex);

      constants.put("carrierName" + sub, carrierName.toString());
      constants.put("displayName" + sub, displayName.toString());
      constants.put("countryCode" + sub, countryIso);
      constants.put("mcc" + sub, mcc);
      constants.put("mnc" + sub, mnc);
      constants.put("isNetworkRoaming" + sub, networkRoaming);
      constants.put("isDataRoaming"    + sub, (dataRoaming == 1));
      constants.put("simSlotIndex"     + sub, simSlotIndex);
      constants.put("phoneNumber"      + sub, number);
      constants.put("deviceId"         + sub, deviceId);
      constants.put("simSerialNumber"  + sub, iccId);
      constants.put("subscriptionId"   + sub, subscriptionId);
      sub++;
    }
  } catch (Exception e) {
    e.printStackTrace();
  }

  return constants;
}
 
Example 15
Source File: NetWorkUtil.java    From NetEasyNews with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 获取当前的网络状态 :没有网络-0:WIFI网络1:4G网络-4:3G网络-3:2G网络-2
 * 自定义
 *
 * @param context
 * @return
 */
public static int getAPNType(Context context) {
    //结果返回值
    int netType = 0;
    //获取手机所有连接管理对象
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    //获取NetworkInfo对象
    NetworkInfo networkInfo = manager.getActiveNetworkInfo();
    //NetworkInfo对象为空 则代表没有网络
    if (networkInfo == null) {
        return netType;
    }
    //否则 NetworkInfo对象不为空 则获取该networkInfo的类型
    int nType = networkInfo.getType();
    if (nType == ConnectivityManager.TYPE_WIFI) {
        //WIFI
        netType = 1;
    } else if (nType == ConnectivityManager.TYPE_MOBILE) {
        int nSubType = networkInfo.getSubtype();
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        //3G   联通的3G为UMTS或HSDPA 电信的3G为EVDO
        if (nSubType == TelephonyManager.NETWORK_TYPE_LTE
                && !telephonyManager.isNetworkRoaming()) {
            netType = 4;
        } else if (nSubType == TelephonyManager.NETWORK_TYPE_UMTS
                || nSubType == TelephonyManager.NETWORK_TYPE_HSDPA
                || nSubType == TelephonyManager.NETWORK_TYPE_EVDO_0
                && !telephonyManager.isNetworkRoaming()) {
            netType = 3;
            //2G 移动和联通的2G为GPRS或EGDE,电信的2G为CDMA
        } else if (nSubType == TelephonyManager.NETWORK_TYPE_GPRS
                || nSubType == TelephonyManager.NETWORK_TYPE_EDGE
                || nSubType == TelephonyManager.NETWORK_TYPE_CDMA
                && !telephonyManager.isNetworkRoaming()) {
            netType = 2;
        } else {
            netType = 2;
        }
    }
    return netType;
}
 
Example 16
Source File: NetworkUtils.java    From MyUtil with Apache License 2.0 4 votes vote down vote up
/**
 * 获取当前的网络状态
 * @param context 全局context
 * @return
 * 没有网络-NO
 * WIFI网络-WIFI
 * 4G网络-4G
 * 3G网络-3G
 * 2G网络-2G
 * 未知-Unknown
 */
public static String getAPNType(Context context) {
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = manager.getActiveNetworkInfo();
    if (networkInfo == null) {      //无网络
        return "NO";
    }

    int nType = networkInfo.getType();
    if (nType == ConnectivityManager.TYPE_WIFI) {       //wifi
        return "WIFI";
    } else if (nType == ConnectivityManager.TYPE_MOBILE) {
        int nSubType = networkInfo.getSubtype();
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (nSubType == TelephonyManager.NETWORK_TYPE_LTE
                && !telephonyManager.isNetworkRoaming()) {
            return "4G";
        } else if (nSubType == TelephonyManager.NETWORK_TYPE_UMTS
                || nSubType == TelephonyManager.NETWORK_TYPE_EVDO_0
                || nSubType == TelephonyManager.NETWORK_TYPE_EVDO_A
                || nSubType == TelephonyManager.NETWORK_TYPE_HSDPA
                || nSubType == TelephonyManager.NETWORK_TYPE_HSUPA
                || nSubType == TelephonyManager.NETWORK_TYPE_HSPA
                || nSubType == TelephonyManager.NETWORK_TYPE_EVDO_B
                || nSubType == TelephonyManager.NETWORK_TYPE_EHRPD
                || nSubType == TelephonyManager.NETWORK_TYPE_HSPAP
                && !telephonyManager.isNetworkRoaming()) {
            return "3G";
        } else if (nSubType == TelephonyManager.NETWORK_TYPE_GPRS
                || nSubType == TelephonyManager.NETWORK_TYPE_EDGE
                || nSubType == TelephonyManager.NETWORK_TYPE_CDMA
                || nSubType == TelephonyManager.NETWORK_TYPE_1xRTT
                || nSubType == TelephonyManager.NETWORK_TYPE_IDEN
                && !telephonyManager.isNetworkRoaming()) {
            return "2G";
        } else {
            return "Unknown";
        }
    }

    return "Unknown";
}
 
Example 17
Source File: NetWorkUtils.java    From ZhihuDaily with Apache License 2.0 4 votes vote down vote up
/**
 * 获取当前的网络状态 :没有网络-0:WIFI网络1:4G网络-4:3G网络-3:2G网络-2
 * 自定义
 *
 * @param context
 * @return
 */
public static int getAPNType(Context context) {
    //结果返回值
    int netType = 0;
    //获取手机所有连接管理对象
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    //获取NetworkInfo对象
    NetworkInfo networkInfo = manager.getActiveNetworkInfo();
    //NetworkInfo对象为空 则代表没有网络
    if (networkInfo == null) {
        return netType;
    }
    //否则 NetworkInfo对象不为空 则获取该networkInfo的类型
    int nType = networkInfo.getType();
    if (nType == ConnectivityManager.TYPE_WIFI) {
        //WIFI
        netType = 1;
    } else if (nType == ConnectivityManager.TYPE_MOBILE) {
        int nSubType = networkInfo.getSubtype();
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        //3G   联通的3G为UMTS或HSDPA 电信的3G为EVDO
        if (nSubType == TelephonyManager.NETWORK_TYPE_LTE
                && !telephonyManager.isNetworkRoaming()) {
            netType = 4;
        } else if (nSubType == TelephonyManager.NETWORK_TYPE_UMTS
                || nSubType == TelephonyManager.NETWORK_TYPE_HSDPA
                || nSubType == TelephonyManager.NETWORK_TYPE_EVDO_0
                && !telephonyManager.isNetworkRoaming()) {
            netType = 3;
            //2G 移动和联通的2G为GPRS或EGDE,电信的2G为CDMA
        } else if (nSubType == TelephonyManager.NETWORK_TYPE_GPRS
                || nSubType == TelephonyManager.NETWORK_TYPE_EDGE
                || nSubType == TelephonyManager.NETWORK_TYPE_CDMA
                && !telephonyManager.isNetworkRoaming()) {
            netType = 2;
        } else {
            netType = 2;
        }
    }
    return netType;
}
 
Example 18
Source File: NetWorkUtils.java    From ToDoList with Apache License 2.0 4 votes vote down vote up
/**
 * 获取当前的网络状态 :没有网络-0:WIFI网络1:4G网络-4:3G网络-3:2G网络-2
 * 自定义
 *
 * @param context
 * @return
 */
public static int getAPNType(Context context) {
    //结果返回值
    int netType = 0;
    //获取手机所有连接管理对象
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    //获取NetworkInfo对象
    NetworkInfo networkInfo = manager.getActiveNetworkInfo();
    //NetworkInfo对象为空 则代表没有网络
    if (networkInfo == null) {
        return netType;
    }
    //否则 NetworkInfo对象不为空 则获取该networkInfo的类型
    int nType = networkInfo.getType();
    if (nType == ConnectivityManager.TYPE_WIFI) {
        //WIFI
        netType = 1;
    } else if (nType == ConnectivityManager.TYPE_MOBILE) {
        int nSubType = networkInfo.getSubtype();
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        //3G   联通的3G为UMTS或HSDPA 电信的3G为EVDO
        if (nSubType == TelephonyManager.NETWORK_TYPE_LTE
                && !telephonyManager.isNetworkRoaming()) {
            netType = 4;
        } else if (nSubType == TelephonyManager.NETWORK_TYPE_UMTS
                || nSubType == TelephonyManager.NETWORK_TYPE_HSDPA
                || nSubType == TelephonyManager.NETWORK_TYPE_EVDO_0
                && !telephonyManager.isNetworkRoaming()) {
            netType = 3;
            //2G 移动和联通的2G为GPRS或EGDE,电信的2G为CDMA
        } else if (nSubType == TelephonyManager.NETWORK_TYPE_GPRS
                || nSubType == TelephonyManager.NETWORK_TYPE_EDGE
                || nSubType == TelephonyManager.NETWORK_TYPE_CDMA
                && !telephonyManager.isNetworkRoaming()) {
            netType = 2;
        } else {
            netType = 2;
        }
    }
    return netType;
}
 
Example 19
Source File: NetworkUtils.java    From shinny-futures-android with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 获取当前的网络状态 :没有网络-0:WIFI网络1:4G网络-4:3G网络-3:2G网络-2
 * 自定义
 */
public static int getAPNType(Context context) {
    //结果返回值
    int netType = 0;
    //获取手机所有连接管理对象
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    //获取NetworkInfo对象
    NetworkInfo networkInfo = manager.getActiveNetworkInfo();
    //NetworkInfo对象为空 则代表没有网络
    if (networkInfo == null) {
        return netType;
    }
    //否则 NetworkInfo对象不为空 则获取该networkInfo的类型
    int nType = networkInfo.getType();
    if (nType == ConnectivityManager.TYPE_WIFI) {
        //WIFI
        netType = 1;
    } else if (nType == ConnectivityManager.TYPE_MOBILE) {
        int nSubType = networkInfo.getSubtype();
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        //3G   联通的3G为UMTS或HSDPA 电信的3G为EVDO
        if (nSubType == TelephonyManager.NETWORK_TYPE_LTE
                && !telephonyManager.isNetworkRoaming()) {
            netType = 4;
        } else if (nSubType == TelephonyManager.NETWORK_TYPE_UMTS
                || nSubType == TelephonyManager.NETWORK_TYPE_HSDPA
                || nSubType == TelephonyManager.NETWORK_TYPE_EVDO_0
                && !telephonyManager.isNetworkRoaming()) {
            netType = 3;
            //2G 移动和联通的2G为GPRS或EGDE,电信的2G为CDMA
        } else if (nSubType == TelephonyManager.NETWORK_TYPE_GPRS
                || nSubType == TelephonyManager.NETWORK_TYPE_EDGE
                || nSubType == TelephonyManager.NETWORK_TYPE_CDMA
                && !telephonyManager.isNetworkRoaming()) {
            netType = 2;
        } else {
            netType = 2;
        }
    }
    return netType;
}
 
Example 20
Source File: NetworkStateUtil.java    From star-zone-android with Apache License 2.0 4 votes vote down vote up
/**
 * 获取当前的网络状态 :没有网络-0:WIFI网络1:4G网络-4:3G网络-3:2G网络-2
 * 自定义
 *
 * @param context
 * @return
 */
public static int getAPNType(Context context) {
    //结果返回值
    int netType = 0;
    //获取手机所有连接管理对象
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    //获取NetworkInfo对象
    NetworkInfo networkInfo = manager.getActiveNetworkInfo();
    //NetworkInfo对象为空 则代表没有网络
    if (networkInfo == null) {
        return netType;
    }
    //否则 NetworkInfo对象不为空 则获取该networkInfo的类型
    int nType = networkInfo.getType();
    if (nType == ConnectivityManager.TYPE_WIFI) {
        //WIFI
        netType = 1;
    } else if (nType == ConnectivityManager.TYPE_MOBILE) {
        int nSubType = networkInfo.getSubtype();
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        //3G   联通的3G为UMTS或HSDPA 电信的3G为EVDO
        if (nSubType == TelephonyManager.NETWORK_TYPE_LTE
                && !telephonyManager.isNetworkRoaming()) {
            netType = 4;
        } else if (nSubType == TelephonyManager.NETWORK_TYPE_UMTS
                || nSubType == TelephonyManager.NETWORK_TYPE_HSDPA
                || nSubType == TelephonyManager.NETWORK_TYPE_EVDO_0
                && !telephonyManager.isNetworkRoaming()) {
            netType = 3;
            //2G 移动和联通的2G为GPRS或EGDE,电信的2G为CDMA
        } else if (nSubType == TelephonyManager.NETWORK_TYPE_GPRS
                || nSubType == TelephonyManager.NETWORK_TYPE_EDGE
                || nSubType == TelephonyManager.NETWORK_TYPE_CDMA
                && !telephonyManager.isNetworkRoaming()) {
            netType = 2;
        } else {
            netType = 2;
        }
    }
    return netType;
}