Cannot resolve public method setFrequencyBand in WifiManager class

╄→гoц情女王★ 提交于 2020-06-16 05:04:50

问题


Good day all

In short, WifiManager.java is a source module given by Google API's for Wifi related functions.

its class declaration :

public class WifiManager {

obviously contains many functions, of which some I am able to access, and no they are not private functions

from the class description:

  • This class provides the primary API for managing all aspects of Wi-Fi connectivity. Get an instance of this class by calling

    {@link android.content.Context#getSystemService(String) Context.getSystemService(Context.WIFI_SERVICE)}.

This calling this getting this WiFi_Service context, casted into a type WiFiManager object:

Context context = this;
        WifiManager wifiManager = (WifiManager) context.getSystemService(context.WIFI_SERVICE);

and attempting to use the required method

from WiFiManager Class:

public static final int WIFI_FREQUENCY_BAND_5GHZ = 1;

thus, calling:

wifiManager.setFrequencyBand(1, true);

results in an error:

Cannot resolve method 'setFrequencyBand(int, boolean)'

Here is method I am able to access from the WifiManager class

public boolean setWifiEnabled(boolean enabled) {
        try {
            return mService.setWifiEnabled(enabled);
        } catch (RemoteException e) {
            return false;
        }
    }

but not this one (of which there are many more):

public void setFrequencyBand(int band, boolean persist) {
        try {
            mService.setFrequencyBand(band, persist);
        } catch (RemoteException e) { }
    }

回答1:


Take a look at the source code of the WifiManager class:

/**
 * Set the operational frequency band.
 * @param band  One of
 *     {@link #WIFI_FREQUENCY_BAND_AUTO},
 *     {@link #WIFI_FREQUENCY_BAND_5GHZ},
 *     {@link #WIFI_FREQUENCY_BAND_2GHZ},
 * @param persist {@code true} if this needs to be remembered
 * @hide
 */
public void setFrequencyBand(int band, boolean persist) {
    try {
        mService.setFrequencyBand(band, persist);
    } catch (RemoteException e) { }
}

This method has the @hide annotation, which means it's part of the hidden API.

You cannot call it directly.

You can use the hidden API by using reflection or modding android.jar, but it is strongly discouraged. They are hidden for a reason. They are not guaranteed to be stable, they can change anytime and your app might easily break in future releases.




回答2:


Take a look at the source code of the setFrequencyBand reflection method.

It is strongly discouraged. They are hidden for a reason. They are not guaranteed to be stable, they can change anytime and your app might easily break in future releases.

/**
 * Auto settings in the driver. The driver could choose to operate on both
 * 2.4 GHz and 5 GHz or make a dynamic decision on selecting the band.
 * @hide
 */
public static final int WIFI_FREQUENCY_BAND_AUTO = 0;
/**
 * Operation on 5 GHz alone
 * @hide
 */
public static final int WIFI_FREQUENCY_BAND_5GHZ = 1;
/**
 * Operation on 2.4 GHz alone
 * @hide
 */
public static final int WIFI_FREQUENCY_BAND_2GHZ = 2;




private void setFrequencyBand(WifiManager wm, int freq, boolean persist)
{
    try
    {
        Class cls = Class.forName("android.net.wifi.WifiManager");
        Method method = cls.getDeclaredMethod("setFrequencyBand", int.class, boolean.class);
        method.invoke(wm, new Integer(freq), new Boolean(persist));
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}


来源:https://stackoverflow.com/questions/37731664/cannot-resolve-public-method-setfrequencyband-in-wifimanager-class

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