简体   繁体   中英

Get current/statechanged cell signal strength. onStateSignalChanged never invoked

GOAL: get current/statechanged cell signal strength.

PROBLEM: reading around (here and on developer.android web site) brought me to this code. But it doesn't work. I mean I've tried to set a breakpoint on onSignalStrengthChanged callback invocation but is never called. I've also declared coarse location permission on android manifest (is needed for getAllCellInfo() .

QUESTIONS:

1) why onSignalStrengthChanged is never called ?

2) I imagine that the aforementioned callback will be invoked just when the signal strength will change. But how can I get the current signal strength before it changes?

@Override
        protected void onCreate(Bundle savedInstanceState) {
 telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
            telephonyManager.listen(new PhoneStateListener(), PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
}

public void onSignalStrengthsChanged(SignalStrength signalStrength){
            try{
                int level;

                for (final CellInfo info : telephonyManager.getAllCellInfo() ){

                    if (info instanceof CellInfoGsm) {
                        final CellSignalStrengthGsm gsm = ((CellInfoGsm) info).getCellSignalStrength();
                        // do what you need
                        level = gsm.getLevel();
                        Toast.makeText(getApplicationContext(), "gsm rssi"+level, Toast.LENGTH_LONG).show();
                    } else if (info instanceof CellInfoCdma) {
                        final CellSignalStrengthCdma cdma = ((CellInfoCdma) info).getCellSignalStrength();
                        // do what you need
                        level = cdma.getLevel();
                        Toast.makeText(getApplicationContext(), "cdma rssi"+level, Toast.LENGTH_LONG).show();
                    } else if (info instanceof CellInfoLte) {
                        final CellSignalStrengthLte lte = ((CellInfoLte) info).getCellSignalStrength();
                        // do what you need
                        level = lte.getLevel();
                        Toast.makeText(getApplicationContext(), "lte rssi"+level, Toast.LENGTH_LONG).show();
                    } else {
                        throw new Exception("Unknown type of cell signal!");
                    }


                }

            }
            catch (Exception e) {

            }

        }

First Define below mentioned variables globally :

TelephonyManager telephonyManager;
MyPhoneStateListener psListener;
TextView txtSignalStr;

onCreate Method :

@Override
protected void onCreate(final Bundle savedInstanceState) {
 psListener = new MyPhoneStateListener();
 telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
 telephonyManager.listen(psListener,PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
}

Create an inner class that will extend PhoneStateListener

and use @Override annotation before onSignalStrengthsChanged method like

Create MyPhoneStateListener Class :

public class MyPhoneStateListener extends PhoneStateListener {
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength)
{
 super.onSignalStrengthsChanged(signalStrength);
//your Code
 }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM