在我的应用中调用setWifiEnabled(true)
时出现此错误:
W/ActivityManager( 3018): Permission Denial: getCurrentUser() from pid=21765, uid=10121 requires android.permission.INTERACT_ACROSS_USERS
W/WifiService( 3018): java.lang.SecurityException: Permission Denial: getCurrentUser() from pid=21765, uid=10121 requires android.permission.INTERACT_ACROSS_USERS
我不知道为什么会这样,因为此方法的specification中说它只需要Manifest.permission.CHANGE_WIFI_STATE
。我知道该方法将在API级别29上弃用,但我的目标是API 24。
我已经检查到这是在Android 5.1.1(API级别22)上发生的,而不是在Android 8.1.0(API级别27)上发生的
我检查了WifiManager的source code,以了解为什么在getCurrentUser()
处被调用,但我找不到它
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");
}
/*
* Caller might not have WRITE_SECURE_SETTINGS,
* only CHANGE_WIFI_STATE is enforced
*/
long ident = Binder.clearCallingIdentity();
try {
if (! mSettingsStore.handleWifiToggled(enable)) {
// Nothing to do if wifi cannot be toggled
return true;
}
} finally {
Binder.restoreCallingIdentity(ident);
}
mWifiController.sendMessage(CMD_WIFI_TOGGLED);
return true;
}
我将此添加到了AndroidManifest.xml中,但是什么也没发生:
<uses-permission android:name="android.permission.INTERACT_ACROSS_USERS"/>