我刚刚开始为Android开发,我正在试图弄清楚如何获得用户信号强度。以下代码为我提供了GSM网络的Dbm:
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
// Get GSM Signal Strength (Dbm)
CellInfoGsm GSM = (CellInfoGsm)telephonyManager.getAllCellInfo().get(0);
CellSignalStrengthGsm cellSignalStrengthGsm = GSM.getCellSignalStrength();
signalStrength = cellSignalStrengthGsm.getDbm();
但是我希望能够从任何类型的网络中获得信号强度。可能是GSM,LTE,CDMA或WCDMA。我查看了电话文档,但我对如何解决此问题感到迷茫。我试图采用与上面相同的代码,只是用Lte替换Gsm,但最终导致模拟器崩溃。
非常感谢任何帮助。
更新并添加了以下答案中的监听器。但是现在,只要我运行应用程序,它就会崩溃。这是日志文件。
05-26 11:25:21.140: D/AndroidRuntime(921): Shutting down VM
05-26 11:25:21.140: W/dalvikvm(921): threadid=1: thread exiting with uncaught exception (group=0xb3aecba8)
05-26 11:25:21.170: E/AndroidRuntime(921): FATAL EXCEPTION: main
05-26 11:25:21.170: E/AndroidRuntime(921): Process: com.example.androidcellinfo, PID: 921
05-26 11:25:21.170: E/AndroidRuntime(921): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidcellinfo/com.example.androidcellinfo.MainActivity}: java.lang.NullPointerException
05-26 11:25:21.170: E/AndroidRuntime(921): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-26 11:25:21.170: E/AndroidRuntime(921): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-26 11:25:21.170: E/AndroidRuntime(921): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-26 11:25:21.170: E/AndroidRuntime(921): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-26 11:25:21.170: E/AndroidRuntime(921): at android.os.Handler.dispatchMessage(Handler.java:102)
05-26 11:25:21.170: E/AndroidRuntime(921): at android.os.Looper.loop(Looper.java:136)
05-26 11:25:21.170: E/AndroidRuntime(921): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-26 11:25:21.170: E/AndroidRuntime(921): at java.lang.reflect.Method.invokeNative(Native Method)
05-26 11:25:21.170: E/AndroidRuntime(921): at java.lang.reflect.Method.invoke(Method.java:515)
05-26 11:25:21.170: E/AndroidRuntime(921): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-26 11:25:21.170: E/AndroidRuntime(921): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-26 11:25:21.170: E/AndroidRuntime(921): at dalvik.system.NativeStart.main(Native Method)
05-26 11:25:21.170: E/AndroidRuntime(921): Caused by: java.lang.NullPointerException
05-26 11:25:21.170: E/AndroidRuntime(921): at com.example.androidcellinfo.CellInfo.<init>(CellInfo.java:74)
05-26 11:25:21.170: E/AndroidRuntime(921): at com.example.androidcellinfo.MainActivity.onCreate(MainActivity.java:28)
05-26 11:25:21.170: E/AndroidRuntime(921): at android.app.Activity.performCreate(Activity.java:5231)
05-26 11:25:21.170: E/AndroidRuntime(921): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-26 11:25:21.170: E/AndroidRuntime(921): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-26 11:25:21.170: E/AndroidRuntime(921): ... 11 more
05-26 11:25:25.090: I/Process(921): Sending signal. PID: 921 SIG: 9
以下是第74行(CellInfo)的代码
以下是第28行的代码(MainActivity)
答案 0 :(得分:0)
重复问题?
How to get cell service signal strength in Android?
Android: How do I get GSM signal strength for all available network operators
Retrieve GSM signal strength in Android
无论如何,请检查上面的链接或这些引用:
http://developer.android.com/reference/android/telephony/SignalStrength.html
http://www.firstdroid.com/2010/05/12/get-provider-gsm-signal-strength/
如果你懒得检查,这似乎有效(没有检查):
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) {}
//Deprecated
//public void onSignalStrengthChanged(int asu)
public void onSignalStrengthChanged (SignalStrength signalStrength)
{
//Do your thing
}
};
创建自己的PhoneStateListener
后,使用位掩码向Telephony Manager
注册,以指示您要侦听的事件,如以下代码段所示:
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);
如何获取所有可用网络的列表:
Android: How do I get GSM signal strength for all available network operators