Java 无法解析WifiManager类中的公共方法setFrequencyBand

Java 无法解析WifiManager类中的公共方法setFrequencyBand,java,android,wifi,wifimanager,Java,Android,Wifi,Wifimanager,大家好 简而言之,WifiManager.java是Google API为Wifi相关功能提供的源模块 其类别声明: public class WifiManager { 显然包含许多函数,其中一些我可以访问,不,它们不是私有函数 从类描述中: 此类提供管理Wi-Fi所有方面的主要API 连通性通过调用 {@link android.content.Context#getSystemService(字符串) Context.getSystemService(Context.WIFI\u SE

大家好

简而言之,WifiManager.java是Google API为Wifi相关功能提供的源模块

其类别声明:

public class WifiManager {
显然包含许多函数,其中一些我可以访问,不,它们不是私有函数

从类描述中:

  • 此类提供管理Wi-Fi所有方面的主要API 连通性通过调用

    {@link android.content.Context#getSystemService(字符串) Context.getSystemService(Context.WIFI\u SERVICE)}

此调用此获取此WiFi_服务上下文,并将其转换为类型WiFiManager对象:

Context context = this;
        WifiManager wifiManager = (WifiManager) context.getSystemService(context.WIFI_SERVICE);
并尝试使用所需的方法

WiFiManager
Class:

public static final int WIFI_FREQUENCY_BAND_5GHZ = 1;
因此,呼吁:

wifiManager.setFrequencyBand(1, true);
导致错误:

无法解析方法“setFrequencyBand(int,boolean)”

下面是我可以从
WifiManager
类访问的方法

public boolean setWifiEnabled(boolean enabled) {
        try {
            return mService.setWifiEnabled(enabled);
        } catch (RemoteException e) {
            return false;
        }
    }
但不是这个(还有很多):


查看
WifiManager
类的源代码:

/**
 * 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) { }
}
此方法具有
@hide
注释,这意味着它是隐藏API的一部分

你不能直接叫它


您可以使用反射或modding
android.jar
来使用隐藏的API,但强烈建议不要这样做。它们被隐藏是有原因的。它们不能保证稳定,可以随时更改,在将来的版本中,您的应用程序可能很容易崩溃。

请查看setFrequencyBand反射方法的源代码

/**
 * 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();
    }
}
这是强烈劝阻。它们被隐藏是有原因的。它们不一定是稳定的,它们可以随时更改,您的应用程序在未来的版本中可能很容易损坏

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

您在哪里发现
WifiManager
中有一个
setFrequencyBand
?政府没有提到这一点