gpt4 book ai didi

java - WifiManager.setWifiEnabled() 究竟返回什么?

转载 作者:行者123 更新时间:2023-11-30 00:35:05 25 4
gpt4 key购买 nike

你们中有人有过 setWifiEnabled() 返回值的经验吗?

文档不清楚,它说:

Returns true if the operation succeeds (or if the existing state is the same as the requested state).

运行失败怎么办?它会抛出异常还是返回 false

是否可以这样做:

if (!WifiManager.setWifiEnabled(true)) {
Log.i(LOG_TAG, "Wifi switch failed.");
} else {
Log.i(LOG_TAG, "Wifi switch succeeded.")
}

非常感谢。

最佳答案

已接受的答案已过时(来源来自 android 4.2,其中当前的 android 现在是 10,11 将很快推出)。此方法可以返回 false。 WifiServiceImpl.java是处理WifiManager#setWifiEnabled(boolean)的电流源:

代码:

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

关于java - WifiManager.setWifiEnabled() 究竟返回什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43564419/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com