不使用PhoneStateListener onSignalStrengthchanged获取SignalStrength

use*_*865 2 android signal-strength phone-state-listener

有没有人知道如何获得信号强度,而无需调用onSignalStrengthChanged.onSignalStrengthchanged的问题在于它在信号强度变化时被调用,我需要根据不同的标准获得signalstrength的值.

提前致谢.

And*_*dre 12

在API级别17 ,这里是一些可以在Activity(或任何其他Context子类)中使用的代码:

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);
}
Run Code Online (Sandbox Code Playgroud)

以前的Android版本要求您调用侦听器,没有其他选择(请参阅此链接).

还要确保您的应用程序包含适当的权限.