我想从我的手机中获取cid,我目前使用以下代码来实现这个目标:
GsmCellLocation gsmLocation = (GsmCellLocation)telephonyManager.getCellLocation();
int Cid = gsmLocation.getCid();
Log.e("#############","current cid is: "+Cid);
int lac = gsmLocation.getLac();
Log.e("#############","current lac is: "+lac);
这将返回类似于301或6061的内容。
我正在浏览一些示例代码,发现了这个:
/**
* Seems that cid and lac shall be in hex. Cid should be padded with zero's
* to 8 numbers if UMTS (3G) cell, otherwise to 4 numbers. Mcc padded to 3
* numbers. Mnc padded to 2 numbers.
*/
try {
// Update the current location
updateLocation(getPaddedHex(cid, cellPadding), getPaddedHex(lac, 4),
getPaddedInt(mnc, 2), getPaddedInt(mcc, 3));
strResult = "Position updated!";
} catch (IOException e) {
strResult = "Error!\n" + e.getMessage();
}
// Show an info Toast with the results of the updateLocation
// call.
Toast t = Toast.makeText(getApplicationContext(), strResult,
Toast.LENGTH_LONG);
t.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
t.show();}});
}
/**
* Convert an int to an hex String and pad with 0's up to minLen.
*/
String getPaddedHex(int nr, int minLen) {
String str = Integer.toHexString(nr);
if (str != null) {
while (str.length() < minLen) {
str = "0" + str;
}
}
return str;
}
/**
* Convert an int to String and pad with 0's up to minLen.
*/
String getPaddedInt(int nr, int minLen) {
String str = Integer.toString(nr);
if (str != null) {
while (str.length() < minLen) {
str = "0" + str;
}
}
return str;
}
GsmCellLocation返回的数字是否正确,或者我需要像示例中展示的那样修改结果?有什么区别吗?我从文档中知道cid可能是-1(未知)或0xffff最大值,0xffff是用于2G或3G网络?