当我请求Cell ID和LAC信息时,在某些设备上我无法检索它们。
我使用此代码:
TelephonyManager tm =(TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
location = (GsmCellLocation) tm.getCellLocation();
cellID = location.getCid();
lac = location.getLac();
答案 0 :(得分:18)
为了找到CellId,你应该使用0xffff作为位掩码,而不是mod。
WRONG
new_cid = cellLocation.getCid() % 0xffff;
RIGHT
new_cid = cellLocation.getCid() & 0xffff;
答案 1 :(得分:2)
尝试使用PhoneStateListener,如下所示:
首先,创建监听器。
public PhoneStateListener phoneStateListener = new PhoneStateListener() {
@Override
public void onCellLocationChanged (CellLocation location) {
StringBuffer str = new StringBuffer();
// GSM
if (location instanceof GsmCellLocation) {
GsmCellLocation loc = (GsmCellLocation) location;
str.append("gsm ");
str.append(loc.getCid());
str.append(" ");
str.append(loc.getLac());
Log.d(TAG, str.toString());
}
}
};
然后在onCreate()上注册,监听器如下:
telephonyManager = (TelephonyManager) getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CELL_LOCATION);
正如documentation所述,LISTEN_CELL_LOCATION要求您添加以下权限:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
答案 2 :(得分:0)
我认为这是由于制造商在设备上实现底层内核代码的方式,而不允许您访问某些信息。
答案 3 :(得分:0)
您需要使用TelephonyManager
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
GsmCellLocation cellLocation = (GsmCellLocation) telephonyManager
.getCellLocation();
// Cell Id, LAC
int cellid = cellLocation.getCid();
int lac = cellLocation.getLac();
// MCC
String MCC = telephonyManager.getNetworkOperator();
int mcc = Integer.parseInt(MCC.substring(0, 3));
// Operator name
String operatoprName = telephonyManager.getNetworkOperatorName();
要获得权限,您需要在Manifest.xml文件中添加followin
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
答案 4 :(得分:-2)
所以你可以尝试类似的东西。我有GSM的小区ID和位置区号。但是对于UMTS,getCid()为exple 33 166 248返回一个大数字。所以我添加了模运算符(exple xXx.getCid()%0xffff)。
GsmCellLocation cellLocation = (GsmCellLocation)telm.getCellLocation();
new_cid = cellLocation.getCid() % 0xffff;
new_lac = cellLocation.getLac() % 0xffff;