下面列出了android.telephony.CellInfoGsm#getCellIdentity ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Converts CellInfoGsm into JSON
* @param cellInfo CellInfoGsm
* @return JSON
*/
public static String cellInfoGSMJSON(CellInfoGsm cellInfo, boolean returnSignalStrength){
final Calendar calendar = Calendar.getInstance();
final JSONObject json = new JSONObject();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2 && cellInfo != null) {
try {
json.put("provider", CELLINFO_PROVIDER);
json.put("type", GSM);
json.put("timestamp", calendar.getTimeInMillis());
final CellIdentityGsm identityGsm = cellInfo.getCellIdentity();
json.put("cid", identityGsm.getCid());
json.put("lac", identityGsm.getLac());
json.put("mcc", identityGsm.getMcc());
json.put("mnc", identityGsm.getMnc());
if (returnSignalStrength){
final JSONObject jsonSignalStrength = new JSONObject();
final CellSignalStrengthGsm cellSignalStrengthGsm = cellInfo.getCellSignalStrength();
jsonSignalStrength.put("asuLevel", cellSignalStrengthGsm.getAsuLevel());
jsonSignalStrength.put("dbm", cellSignalStrengthGsm.getDbm());
jsonSignalStrength.put("level", cellSignalStrengthGsm.getLevel());
json.put("cellSignalStrengthGsm", jsonSignalStrength);
}
}
catch(JSONException exc) {
logJSONException(exc);
}
}
return json.toString();
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private void startScan() {
logMessage("starting tower scan... ");
Scan scan = new Scan();
// TODO: Get location from user?
scan.setLocation(lastScanName);
// TODO: use actual GPS Coordinates
scan.setLatitude(lastScanLat);
scan.setLongitude(lastScanLon);
long scan_id = db.createScan(scan);
List<CellInfo> cellInfos = (List<CellInfo>) this.telephonyManager
.getAllCellInfo();
// TODO: better error handling of null cellinfos
if (cellInfos != null) {
for (CellInfo cellInfo : cellInfos) {
if (cellInfo instanceof CellInfoGsm) {
CellInfoGsm cellInfoGsm = (CellInfoGsm) cellInfo;
CellIdentityGsm cellIdentity = cellInfoGsm
.getCellIdentity();
CellSignalStrengthGsm cellSignalStrengthGsm = cellInfoGsm
.getCellSignalStrength();
int dbmLevel = cellSignalStrengthGsm.getDbm();
com.spideyapp.sqlite.model.CellInfo cell = new com.spideyapp.sqlite.model.CellInfo(
cellIdentity.getCid(), cellIdentity.getLac(),
cellIdentity.getMcc(), cellIdentity.getMnc(),dbmLevel);
db.createCell(cell, scan_id);
shareCellInfo (cell);
}
}
}
}
/**
* Adds or updates a cell tower.
* <p>
* If the cell tower is already in the list, its data is updated; if not, a
* new entry is created.
* <p>
* This method will set the cell's identity data, its signal strength and
* whether it is the currently serving cell. If the API level is 18 or
* higher, it will also set the generation.
* @return The new or updated entry.
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public CellTowerGsm update(CellInfoGsm cell) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1)
return null;
CellIdentityGsm cid = cell.getCellIdentity();
CellTowerGsm result = null;
CellTowerGsm cand = this.get(cid.getMcc(), cid.getMnc(), cid.getLac(), cid.getCid());
if ((cand != null) && CellTower.matches(cid.getPsc(), cand.getPsc()))
result = cand;
if (result == null) {
cand = this.get(cid.getPsc());
if ((cand != null)
&& ((cid.getMcc() == Integer.MAX_VALUE) || CellTower.matches(cid.getMcc(), cand.getMcc()))
&& ((cid.getMnc() == Integer.MAX_VALUE) || CellTower.matches(cid.getMnc(), cand.getMnc()))
&& ((cid.getLac() == Integer.MAX_VALUE) || CellTower.matches(cid.getLac(), cand.getLac()))
&& ((cid.getCid() == Integer.MAX_VALUE) ||CellTower.matches(cid.getCid(), cand.getCid())))
result = cand;
}
if (result == null)
result = new CellTowerGsm(cid.getMcc(), cid.getMnc(), cid.getLac(), cid.getCid(), cid.getPsc());
if (result.getMcc() == CellTower.UNKNOWN)
result.setMcc(cid.getMcc());
if (result.getMnc() == CellTower.UNKNOWN)
result.setMnc(cid.getMnc());
if (result.getLac() == CellTower.UNKNOWN)
result.setLac(cid.getLac());
if (result.getCid() == CellTower.UNKNOWN)
result.setCid(cid.getCid());
if (result.getPsc() == CellTower.UNKNOWN)
result.setPsc(cid.getPsc());
this.put(result.getText(), result);
this.put(result.getAltText(), result);
result.setCellInfo(true);
result.setDbm(cell.getCellSignalStrength().getDbm());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2)
result.setGeneration(2);
result.setServing(cell.isRegistered());
if ((result.getText() == null) && (result.getAltText() == null))
Log.d(this.getClass().getSimpleName(), String.format("Added %d G cell with no data from CellInfoGsm", result.getGeneration()));
return result;
}