简体   繁体   中英

Cannot resolve public method setFrequencyBand in WifiManager class

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) { }
    }

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.

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();
    }
}

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