如何在Android中获取手机服务信号强度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1967136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get cell service signal strength in Android?
提问by Doughy
I am trying to write a very simple Android application that checks the signal strength of the current cell. So far, I have only found something called getNeighboringCellInfo()
, but I'm not really sure if that includes the current cell.
我正在尝试编写一个非常简单的 Android 应用程序来检查当前单元格的信号强度。到目前为止,我只找到了一个叫做 的东西getNeighboringCellInfo()
,但我不确定它是否包括当前的单元格。
How do I get the CURRENT cell signal strength in Android?
如何在 Android 中获取 CURRENT 细胞信号强度?
Does getNeighborCellInfo()
get the current cell? It doesn't seem like it based on the results that I have been able to get with it. Here's my current code:
是否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);
采纳答案by jspcal
create a PhoneStateListenerand handle the onSignalStrengthChanged callback. When your app is initialized, it should give you an initial notification. This is in 1.x. in 2.x, there's an open issueabout this.
创建一个PhoneStateListener并处理 onSignalStrengthChanged 回调。当您的应用程序初始化时,它应该给您一个初始通知。这是在 1.x 中。在 2.x 中,有一个关于此的未决问题。
回答by tunc olcay
This code may help:
此代码可能有帮助:
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) {}
};
Once you've created your own Phone State Listener, register it with the Telephony Manager using a bitmask to indicate the events you want to listen for, as shown in the following code snippet:
创建自己的电话状态侦听器后,使用位掩码将其注册到电话管理器以指示要侦听的事件,如以下代码段所示:
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);
Also, need to add these to AndroidManifest.xml:
另外,需要将这些添加到 AndroidManifest.xml 中:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>