我试图得到以下数据:
- 基站:的CellID和RSS(识别哪一个是基站)
- 对于所有neigbouring站:的CellID和RSS
有各种各样的API,它看起来像我不得不使用不同的API telephonyManager和PhoneStateListener。 我是一个littlebit困惑,因为我认为这应该在一个界面中提供。 此外,我认为它应该是可能的查询,而不必听国家的变化来判断int当前基站的CellID,因为周围基站CAL也可以从telephonyManager调查。
你能告诉我怎样才能得到上述规定的数据?
新斯库尔路:API 17更漂亮和更清洁,但还是太新。
List<CellInfo> cellInfos = (List<CellInfo>) this.telephonyManager.getAllCellInfo();
for(CellInfo cellInfo : cellInfos)
{
CellInfoGsm cellInfoGsm = (CellInfoGsm) cellInfo;
CellIdentityGsm cellIdentity = cellInfoGsm.getCellIdentity();
CellSignalStrengthGsm cellSignalStrengthGsm = cellInfoGsm.getCellSignalStrength();
Log.d("cell", "registered: "+cellInfoGsm.isRegistered());
Log.d("cell", cellIdentity.toString());
Log.d("cell", cellSignalStrengthGsm.toString());
}
旧的API并没有提供一个非常令人满意的解决方案。 但这里的老斯库尔方式:
this.telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
this.phoneStateListener = setupPhoneStateListener();
this.telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CELL_LOCATION);
this.telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);
this.telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
// This part is used to listen for properties of the neighboring cells
List<NeighboringCellInfo> neighboringCellInfos = this.telephonyManager.getNeighboringCellInfo();
for(NeighboringCellInfo neighboringCellInfo : neighboringCellInfos)
{
neighboringCellInfo.getCid();
neighboringCellInfo.getLac();
neighboringCellInfo.getPsc();
neighboringCellInfo.getNetworkType();
neighboringCellInfo.getRssi();
Log.d("cellp",neighboringCellInfo.toString());
}
public PhoneStateListener setupPhoneStateListener()
{
return new PhoneStateListener() {
/** Callback invoked when device cell location changes. */
@SuppressLint("NewApi")
public void onCellLocationChanged(CellLocation location)
{
GsmCellLocation gsmCellLocation = (GsmCellLocation) location;
gsmCellLocation.getCid();
gsmCellLocation.getLac();
gsmCellLocation.getPsc();
Log.d("cellp", "registered: "+gsmCellLocation.toString());
}
/** invoked when data connection state changes (only way to get the network type) */
public void onDataConnectionStateChanged(int state, int networkType)
{
Log.d("cellp", "registered: "+networkType);
}
/** Callback invoked when network signal strengths changes. */
public void onSignalStrengthsChanged(SignalStrength signalStrength)
{
Log.d("cellp", "registered: "+signalStrength.getGsmSignalStrength());
}
};
}
(请注意,我用的是三星手机,这是一个已知的问题,三星手机不支持相邻小区的列表)