android cell id

时间:2010-10-07 14:52:13

标签: android

我想通过手机获取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网络?

1 个答案:

答案 0 :(得分:2)

该代码并没有真正改变数字。它只是以十六进制或左侧更多的零显示它们。