Code example for TelephonyManager

Methods: getCellLocation, getNetworkOperator, getNetworkType

0
		int cid = 0;
 
		/** 调用API获取基站信息 */ 
		TelephonyManager mTelNet = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		int type = mTelNet.getNetworkType();
 
		// 中国电信为CTC: NETWORK_TYPE_EVDO_A是中国电信3G的getNetworkType; 
		// NETWORK_TYPE_CDMA电信2G是CDMA 
		if (type == TelephonyManager.NETWORK_TYPE_EVDO_A
				|| type == TelephonyManager.NETWORK_TYPE_CDMA
				|| type == TelephonyManager.NETWORK_TYPE_1xRTT) {
			CdmaCellLocation location = (CdmaCellLocation) mTelNet
					.getCellLocation();
 
			StringBuilder nsb = new StringBuilder();
			nsb.append(location.getSystemId());
 
			cid = location.getBaseStationId();
			lac = location.getNetworkId();
			mnc = location.getSystemId();
			mcc = Integer.valueOf(mTelNet.getNetworkOperator().substring(0, 3));
 
		} 
		// 移动2G卡 + CMCC + 2 type = NETWORK_TYPE_EDGE 
		else if (type == TelephonyManager.NETWORK_TYPE_EDGE) {
			GsmCellLocation location = (GsmCellLocation) mTelNet
					.getCellLocation();
			String operator = mTelNet.getNetworkOperator();
 
			cid = location.getCid();
			mcc = Integer.parseInt(operator.substring(0, 3));
			mnc = Integer.parseInt(operator.substring(3));
			lac = location.getLac();
 
		} 
		// 联通的2G经过测试 China Unicom 1 NETWORK_TYPE_GPRS 
		// 经过测试,获取联通数据的时候,无法获取国家代码和网络号码,错误类型为JSON Parsing Error 
		else if (type == TelephonyManager.NETWORK_TYPE_GPRS) {
			GsmCellLocation location = (GsmCellLocation) mTelNet
					.getCellLocation();
			String operator = mTelNet.getNetworkOperator();
			cid = location.getCid();
			lac = location.getLac();
			mcc = Integer.parseInt(operator.substring(0, 3));
			mnc = Integer.parseInt(operator.substring(3));
		}