Android:获取Base Station和Neigboring Cells的CellID和RSS

前端之家收集整理的这篇文章主要介绍了Android:获取Base Station和Neigboring Cells的CellID和RSS前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试获取以下数据:

>基站:CellID和RSS(识别哪一个是基站)
>对于所有neigbouring站:CellID和RSS

有各种API,看起来我必须使用不同的API telephonyManager和PhoneStateListener.我有点困惑,因为我认为这应该在一个界面中可用.此外,我认为应该可以轮询当前基站的CellID,而不必监听状态更改以确定int,因为也可以从telephonyManager轮询相邻的Cell Stations.

你能告诉我如何获得上面指定的数据吗?

解决方法

新的skool-way:API 17更好更清洁,但仍然太新.
  1. List<CellInfo> cellInfos = (List<CellInfo>) this.telephonyManager.getAllCellInfo();
  2.  
  3. for(CellInfo cellInfo : cellInfos)
  4. {
  5. CellInfoGsm cellInfoGsm = (CellInfoGsm) cellInfo;
  6.  
  7. CellIdentityGsm cellIdentity = cellInfoGsm.getCellIdentity();
  8. CellSignalStrengthGsm cellSignalStrengthGsm = cellInfoGsm.getCellSignalStrength();
  9.  
  10. Log.d("cell","registered: "+cellInfoGsm.isRegistered());
  11. Log.d("cell",cellIdentity.toString());
  12. Log.d("cell",cellSignalStrengthGsm.toString());
  13. }

旧的API不能提供非常令人满意的解决方案.但这是一个古老的方式:

  1. this.telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
  2. this.phoneStateListener = setupPhoneStateListener();
  3. this.telephonyManager.listen(phoneStateListener,PhoneStateListener.LISTEN_CELL_LOCATION);
  4. this.telephonyManager.listen(phoneStateListener,PhoneStateListener.LISTEN_DATA_CONNECTION_STATE);
  5. this.telephonyManager.listen(phoneStateListener,PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
  6.  
  7. // This part is used to listen for properties of the neighboring cells
  8. List<NeighboringCellInfo> neighboringCellInfos = this.telephonyManager.getNeighboringCellInfo();
  9. for(NeighboringCellInfo neighboringCellInfo : neighboringCellInfos)
  10. {
  11. neighboringCellInfo.getCid();
  12. neighboringCellInfo.getLac();
  13. neighboringCellInfo.getPsc();
  14. neighboringCellInfo.getNetworkType();
  15. neighboringCellInfo.getRSSi();
  16.  
  17. Log.d("cellp",neighboringCellInfo.toString());
  18. }
  19.  
  20. public PhoneStateListener setupPhoneStateListener()
  21. {
  22. return new PhoneStateListener() {
  23.  
  24. /** Callback invoked when device cell location changes. */
  25. @SuppressLint("NewApi")
  26. public void onCellLocationChanged(CellLocation location)
  27. {
  28. GsmCellLocation gsmCellLocation = (GsmCellLocation) location;
  29. gsmCellLocation.getCid();
  30. gsmCellLocation.getLac();
  31. gsmCellLocation.getPsc();
  32.  
  33. Log.d("cellp","registered: "+gsmCellLocation.toString());
  34. }
  35.  
  36. /** invoked when data connection state changes (only way to get the network type) */
  37. public void onDataConnectionStateChanged(int state,int networkType)
  38. {
  39. Log.d("cellp","registered: "+networkType);
  40. }
  41.  
  42. /** Callback invoked when network signal strengths changes. */
  43. public void onSignalStrengthsChanged(SignalStrength signalStrength)
  44. {
  45. Log.d("cellp","registered: "+signalStrength.getGsmSignalStrength());
  46. }
  47.  
  48. };
  49. }

>不要忘记设置所有必要的权限
这个解决方案的问题是,我没有得到任何相邻的单元格信息,尽管我设置:

uses-permission android:name =“android.permission.ACCESS_COARSE_UPDATES”

(请注意,我使用的是三星手机,这是三星手机不支持相邻手机列表的已知问题)

猜你在找的Android相关文章