首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >WifiManager.setWifiEnabled()到底返回什么?

WifiManager.setWifiEnabled()到底返回什么?
EN

Stack Overflow用户
提问于 2017-04-22 20:45:29
回答 2查看 2.9K关注 0票数 1

你们中有谁有使用setWifiEnabled()的返回值的经验吗?

文件不清楚,它说:

如果操作成功(或者现有状态与请求状态相同),则返回true

手术失败时它会做什么?它是抛出异常还是返回false

是否可以做这样的事情:

代码语言:javascript
运行
复制
if (!WifiManager.setWifiEnabled(true)) {
    Log.i(LOG_TAG, "Wifi switch failed.");
} else {
    Log.i(LOG_TAG, "Wifi switch succeeded.")
}

非常感谢。

EN

Stack Overflow用户

发布于 2020-02-22 19:53:40

这个被接受的答案已经过时了(消息来源是Android4.2,目前的android是10,11很快就会发布)。此方法可以返回false。WifiServiceImpl.java是当前处理WifiManager#setWifiEnabled(布尔型)的源。

代码:

代码语言:javascript
运行
复制
 /**
 * see {@link android.net.wifi.WifiManager#setWifiEnabled(boolean)}
 * @param enable {@code true} to enable, {@code false} to disable.
 * @return {@code true} if the enable/disable operation was
 *         started or is already in the queue.
 */
@Override
public synchronized boolean setWifiEnabled(String packageName, boolean enable) {
    if (enforceChangePermission(packageName) != MODE_ALLOWED) {
        return false;
    }
    boolean isPrivileged = isPrivileged(Binder.getCallingPid(), Binder.getCallingUid());
    if (!isPrivileged && !isDeviceOrProfileOwner(Binder.getCallingUid())
            && !mWifiPermissionsUtil.isTargetSdkLessThan(packageName, Build.VERSION_CODES.Q,
              Binder.getCallingUid())
            && !isSystem(packageName, Binder.getCallingUid())) {
        mLog.info("setWifiEnabled not allowed for uid=%")
                .c(Binder.getCallingUid()).flush();
        return false;
    }
    // If Airplane mode is enabled, only privileged apps are allowed to toggle Wifi
    if (mSettingsStore.isAirplaneModeOn() && !isPrivileged) {
        mLog.err("setWifiEnabled in Airplane mode: only Settings can toggle wifi").flush();
        return false;
    }
    // If SoftAp is enabled, only privileged apps are allowed to toggle wifi
    boolean apEnabled = mWifiApState == WifiManager.WIFI_AP_STATE_ENABLED;
    if (apEnabled && !isPrivileged) {
        mLog.err("setWifiEnabled SoftAp enabled: only Settings can toggle wifi").flush();
        return false;
    }
    // If we're in crypt debounce, ignore any wifi state change APIs.
    if (mFrameworkFacade.inStorageManagerCryptKeeperBounce()) {
        return false;
    }
    mLog.info("setWifiEnabled package=% uid=% enable=%").c(packageName)
            .c(Binder.getCallingUid()).c(enable).flush();
    long ident = Binder.clearCallingIdentity();
    try {
        if (!mSettingsStore.handleWifiToggled(enable)) {
            // Nothing to do if wifi cannot be toggled
            return true;
        }
    } finally {
        Binder.restoreCallingIdentity(ident);
    }
    mWifiMetrics.incrementNumWifiToggles(isPrivileged, enable);
    mWifiController.sendMessage(CMD_WIFI_TOGGLED);
    return true;
}
票数 2
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43564419

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档