你们中有谁有使用setWifiEnabled()
的返回值的经验吗?
文件不清楚,它说:
如果操作成功(或者现有状态与请求状态相同),则返回true。
手术失败时它会做什么?它是抛出异常还是返回false
是否可以做这样的事情:
if (!WifiManager.setWifiEnabled(true)) {
Log.i(LOG_TAG, "Wifi switch failed.");
} else {
Log.i(LOG_TAG, "Wifi switch succeeded.")
}
非常感谢。
发布于 2017-04-22 20:59:59
查看WifiService#setWifiEnabled(布尔型)的实现
/**
* 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.
*/
public synchronized boolean setWifiEnabled(boolean enable) {
enforceChangePermission();
Slog.d(TAG, "setWifiEnabled: " + enable + " pid=" + Binder.getCallingPid()
+ ", uid=" + Binder.getCallingUid());
if (DBG) {
Slog.e(TAG, "Invoking mWifiStateMachine.setWifiEnabled\n");
}
if (enable) {
reportStartWorkSource();
}
mWifiStateMachine.setWifiEnabled(enable);
/*
* Caller might not have WRITE_SECURE_SETTINGS,
* only CHANGE_WIFI_STATE is enforced
*/
long ident = Binder.clearCallingIdentity();
try {
handleWifiToggled(enable);
} finally {
Binder.restoreCallingIdentity(ident);
}
if (enable) {
if (!mIsReceiverRegistered) {
registerForBroadcasts();
mIsReceiverRegistered = true;
}
} else if (mIsReceiverRegistered) {
mContext.unregisterReceiver(mReceiver);
mIsReceiverRegistered = false;
}
return true;
}
因此,它永远不会返回false
。如果由于某种原因所需操作无法成功,则将引发异常,请参阅WifiManager.setWifiEnabled(布尔型)
/**
* Enable or disable Wi-Fi.
* @param enabled {@code true} to enable, {@code false} to disable.
* @return {@code true} if the operation succeeds (or if the existing state
* is the same as the requested state).
*/
public boolean setWifiEnabled(boolean enabled) {
try {
return mService.setWifiEnabled(enabled);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
发布于 2020-02-22 19:53:40
这个被接受的答案已经过时了(消息来源是Android4.2,目前的android是10,11很快就会发布)。此方法可以返回false。WifiServiceImpl.java是当前处理WifiManager#setWifiEnabled(布尔型)的源。
代码:
/**
* 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;
}
https://stackoverflow.com/questions/43564419
复制相似问题