我正在尝试编写一个非常简单的Android应用程序来检查当前单元格的信号强度。到目前为止,我只找到了一个名为getNeighboringCellInfo()
的东西,但我不确定它是否包含当前的单元格。
如何在Android中获得CURRENT信元强度?
getNeighborCellInfo()
获取当前单元格吗?它似乎不是基于我能够得到它的结果。这是我目前的代码:
List<NeighboringCellInfo> n = tm.getNeighboringCellInfo();
//Construct the string
String s = "";
int rss = 0;
int cid = 0;
for (NeighboringCellInfo nci : n)
{
cid = nci.getCid();
rss = -113 + 2*nci.getRssi();
s += "Cell ID: " + Integer.toString(cid) + " Signal Power (dBm): " +
Integer.toString(rss) + "\n";
}
mainText.setText(s);
答案 0 :(得分:19)
创建一个PhoneStateListener并处理onSignalStrengthChanged回调。当您的应用初始化时,它应该给您初始通知。这是1.x.在2.x中,有关于此的开放issue。
答案 1 :(得分:13)
此代码可能有所帮助:
PhoneStateListener phoneStateListener = new PhoneStateListener() {
public void onCallForwardingIndicatorChanged(boolean cfi) {}
public void onCallStateChanged(int state, String incomingNumber) {}
public void onCellLocationChanged(CellLocation location) {}
public void onDataActivity(int direction) {}
public void onDataConnectionStateChanged(int state) {}
public void onMessageWaitingIndicatorChanged(boolean mwi) {}
public void onServiceStateChanged(ServiceState serviceState) {}
public void onSignalStrengthChanged(int asu) {}
};
创建自己的电话状态侦听器后,使用位掩码将其注册到Telephony Manager,以指示您要侦听的事件,如以下代码段所示:
TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phoneStateListener,
PhoneStateListener.LISTEN_CALL_FORWARDING_INDICATOR |
PhoneStateListener.LISTEN_CALL_STATE |
PhoneStateListener.LISTEN_CELL_LOCATION |
PhoneStateListener.LISTEN_DATA_ACTIVITY |
PhoneStateListener.LISTEN_DATA_CONNECTION_STATE |
PhoneStateListener.LISTEN_MESSAGE_WAITING_INDICATOR |
PhoneStateListener.LISTEN_SERVICE_STATE |
PhoneStateListener.LISTEN_SIGNAL_STRENGTH);
另外,需要将这些添加到AndroidManifest.xml:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>