Android 获取基站信息

转:http://blog.sina.com.cn/s/blog_6e7dfa3d010184j7.html


Android 基站分CdmaCellLocation和GsmCellLocation,要根据不同的SIM卡转成不同的对象
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
   int type = tm.getNetworkType();//获取网络类型
//在中国,移动的2G是EGDE,联通的2G为GPRS,电信的2G为CDMA,电信的3G为EVDO

Location loc = null;

ArrayList CellID = new ArrayList();

//中国电信为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)

{

location = (CdmaCellLocation) tm.getCellLocation();
if(location == null)
throw new Exception("errr");
String operator = tm.getNetworkOperator();
 int cellIDs = location.getBaseStationId();
  	int networkID = location.getNetworkId();
  	StringBuilder sb = new StringBuilder();
 sb.append(location.getSystemId());
int mnc = Integer.parseInt(nsb.toString());
int mcc = Integer.parseInt(operator.substring(0,3));
 
 

}

//移动2G卡 + CMCC + 2

/pe = NETWORK_TYPE_EDGE

//联通的2G经过测试 China Unicom 1 NETWORK_TYPE_GPRS

else if(type == TelephonyManager.NETWORK_TYPE_EDGE || type == TelephonyManager.NETWORK_TYPE_GPRS)

{

GsmCellLocation location = (GsmCellLocation)tm.getCellLocation();

location = (CdmaCellLocation) tm.getCellLocation();
if(location == null)
throw new Exception("errr");
String operator = tm.getNetworkOperator();
int mcc = Integer.parseInt(operator.substring(0,3));
int mnc = Integer.parseInt(operator.substring(3));
int cid = location.getCid();
int lac = location.getLac();

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
我了解你的要求,下面是获取手机基站信息的最新代码: ``` TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // 如果没有获得权限,则请求权限 ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, MY_PERMISSIONS_REQUEST_LOCATION); return; } // 获取基站信息 CellLocation cellLocation = telephonyManager.getCellLocation(); if (cellLocation instanceof GsmCellLocation) { GsmCellLocation gsmCellLocation = (GsmCellLocation) cellLocation; int cid = gsmCellLocation.getCid(); int lac = gsmCellLocation.getLac(); int mcc = Integer.parseInt(telephonyManager.getNetworkOperator().substring(0, 3)); int mnc = Integer.parseInt(telephonyManager.getNetworkOperator().substring(3)); // 打印基站信息 Log.d(TAG, "CID: " + cid); Log.d(TAG, "LAC: " + lac); Log.d(TAG, "MCC: " + mcc); Log.d(TAG, "MNC: " + mnc); } else if (cellLocation instanceof CdmaCellLocation) { // CDMA 网络,暂不处理 } else { // 无法获取基站信息 Log.d(TAG, "无法获取基站信息"); } ``` 这段代码可以通过调用 TelephonyManager 类中的getCellLocation() 方法来获取手机的基站信息,如果是 GSM 网络,则强转为 GsmCellLocation 类,获取基站 ID(cid),位置区域码(lac)、移动国家代码(mcc)和移动网络代码(mnc)等信息,并打印出来。需要注意的是,该方法需要获取 ACCESS_COARSE_LOCATION 权限才能使用,因此在调用之前需要先检查是否已经获取了该权限。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值