How to get cell service signal strength in Android?

匿名 (未验证) 提交于 2019-12-03 02:11:02

问题:

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.

How do I get the CURRENT cell signal strength in Android?

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:

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); 

回答1:

create a PhoneStateListener and 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 issue about this.



回答2:

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:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!