安卓开发调用库函数出现的“Cannot resolve method xxx”问题

Android Studio新人学习笔记啦

原本是要在Android 8.1上做一个应用来开启无线热点,目标是实现在启动应用后自动打开无线共享热点,根据网上各方的资料决定通过WifiManager操作热点的接口,通过分析Settings的源码来自己编写。

问题出现

在学习Settings中开启wifi热点问题调用mWifimanager的setWifiApConfiguration方法时出现了问题在这里插入图片描述

去网上找了各方的资料后根据查到的资料清空了cache亦无用。

点击菜单中的 “File” -> “Invalidate Caches / Restart”,然后点击对话框中的 “Invalidate and Restart”,清空 cache 并且重启。

无奈回WifiManager去看源码,偶然间发现可调用的方法与不可调用的方法在源代码中描写的方式是有所区别的

可调用的方法:

    /**
     * Enable or disable Wi-Fi.
     * 

* Applications must have the {@link android.Manifest.permission#CHANGE_WIFI_STATE} * permission to toggle wifi. * * @param enabled {@code true} to enable, {@code false} to disable. * @return {@code false} if the request cannot be satisfied; {@code true} indicates that wifi is * either already in the requested state, or in progress toward the requested state. * @throws {@link java.lang.SecurityException} if the caller is missing required permissions. */ public boolean setWifiEnabled(boolean enabled) { try { return mService.setWifiEnabled(mContext.getOpPackageName(), enabled); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } }

无法调用的方法:

    /**
     * Sets the Wi-Fi AP Configuration.  The AP configuration must either be open or
     * WPA2 PSK networks.
     * @return {@code true} if the operation succeeded, {@code false} otherwise
     *
     * @hide
     */
    @SystemApi
    @RequiresPermission(android.Manifest.permission.CHANGE_WIFI_STATE)
    public boolean setWifiApConfiguration(WifiConfiguration wifiConfig) {
        try {
            return mService.setWifiApConfiguration(wifiConfig, mContext.getOpPackageName());
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }

注意到没有,在定义之前无法调用的方法多了个@SystemApi
原来这些函数在调用的时候需要有系统级的权限啊,不懂怎么搞,那只能回到安卓8.1的源码中去直接修改Settings的源码,然后再编译安卓源码啦。

Settings 源码解析:
https://blog.csdn.net/zhangbijun1230/article/details/79954939

你可能感兴趣的:(Android,Stuidio学习笔记)