Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
262 views
in Technique[技术] by (71.8m points)

android - get SignalStrength without Using PhoneStateListener onSignalStrengthchanged

does anyone know how to get the signal strength without having to call the onSignalStrengthChanged. The problem with onSignalStrengthchanged is that is it called when the signal strength changes and I need to get the value of signalstrength according to a different criteria.

Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

On API level 17 only, here's some code that can be used in an Activity (or any other Context child class):

import android.telephony.CellInfo;
import android.telephony.CellInfoCdma;
import android.telephony.CellInfoGsm;
import android.telephony.CellInfoLte;
import android.telephony.CellSignalStrengthCdma;
import android.telephony.CellSignalStrengthGsm;
import android.telephony.CellSignalStrengthLte;
import android.telephony.TelephonyManager;

try {
    final TelephonyManager tm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
    for (final CellInfo info : tm.getAllCellInfo()) {
        if (info instanceof CellInfoGsm) {
            final CellSignalStrengthGsm gsm = ((CellInfoGsm) info).getCellSignalStrength();
            // do what you need
        } else if (info instanceof CellInfoCdma) {
            final CellSignalStrengthCdma cdma = ((CellInfoCdma) info).getCellSignalStrength();
            // do what you need
        } else if (info instanceof CellInfoLte) {
            final CellSignalStrengthLte lte = ((CellInfoLte) info).getCellSignalStrength();
            // do what you need
        } else {
            throw new Exception("Unknown type of cell signal!");
        }
    }
} catch (Exception e) {
    Log.e(TAG, "Unable to obtain cell signal information", e);
}

Previous versions of Android require you to call the listener, there is no other alternative (see this link).

Also ensure that your application contains the appropriate permissions.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...