SignalStrength - getSignalStrength() - getLevel()

馋奶兔 提交于 2021-02-07 20:29:35

问题


I can easily get the sigalStrength in Android via callback

 onSignalStrengthsChanged(SignalStrength signalStrength)

and retrieve the signalStrength trough the passed object

int signal_strength = signalStrength.getGsmSignalStrength();

and according to the documentation the value varies between 0 and 39.99

Now I want to display this in my app in an indicator that updates as the signalStrenght varies - exactly what you can see in the statusbar on your phone.

So my question - how can I use the variable for this? If its linear its easy to just use intervalls from 1 - 10, 11 - 20 etc. But I guess its not that easy?

I know I can standardize this value just through a call to ..

 int level = signalStrength.getLevel()

That is by calling getLevel then I gets a value between 0 - 4, just as RSSI does for WIFI. But the problem is that it requires api 23 and that means I can only reach approx 40% of the android market.

So - If I do not want to use getLevel, how could I use getGsmSignalStrength() accordingly?

  @Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
    super.onSignalStrengthsChanged(signalStrength);

    int signal_strength = signalStrength.getGsmSignalStrength();

    //int level = signalStrength.getLevel(); //api 23

    Toast.makeText(context, "signalStrength: " + signal_strength, Toast.LENGTH_SHORT).show();
}

回答1:


Try following code snippet in your onSignalStrengthsChanged. I achieved it using reflection. It works for all type of network classes. Hope it helps.

@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
    super.onSignalStrengthsChanged(signalStrength);

    int level = 0;
    try {
        final Method m = SignalStrength.class.getDeclaredMethod("getLevel", (Class[]) null);
        m.setAccessible(true);
        level = (Integer) m.invoke(signalStrength, (Object[]) null);
    } catch (Exception e) {
        e.printStackTrace();
    }          
}


来源:https://stackoverflow.com/questions/48711159/signalstrength-getsignalstrength-getlevel

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