我正在尝试获取以下数据:
有各种API,看起来我必须使用不同的API telephonyManager和PhoneStateListener。我有点困惑,因为我认为这应该在一个界面中可用。此外,我认为应该可以轮询当前基站的CellID,而不必监听状态变化以确定int,因为也可以从telephonyManager轮询邻近单元站cal。
你能告诉我如何获得上面指定的数据吗?
答案 0 :(得分:6)
新的skool-way: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());
}
};
}
不要忘记设置所有必要的权限 这个解决方案的问题是,我没有得到任何相邻的单元格信息,尽管我设置了:
uses-permission android:name =“android.permission.ACCESS_COARSE_UPDATES”
(请注意,我使用的是三星手机,这是三星手机不支持相邻手机列表的已知问题)