First put a conclusion
Delete other user's method
DevicePolicyManager dpm = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
ComponentName mName = new ComponentName(this, TestMadmin.class);
for(int i=1;i<=100;i++){
try {
UserHandle userHandle = UserHandle.getUserHandleForUid(i);
//需要设备管理器权限
dpm.removeUser(mName ,userHandle );
} catch (Exception e) {
e.printStackTrace();
}
}
What are the two multi-user
Described random looking for more than one user function
https://baijiahao.baidu.com/s?id=1631589461286423218&wfr=spider&for=pc
effect similar system to open, the user data between two separate, independently of each other
How to delete three other users
DevicePolicyManager has a method:
public boolean removeUser(@NonNull ComponentName admin, UserHandle userHandle)
So the first idea is to traverse all UserHandle, and then click Delete.
Try to use the actual operation
UserManager systemService = (UserManager) getSystemService(Context.USER_SERVICE);
List<UserHandle> userProfiles = systemService.getUserProfiles();
Log.i("test","userProfiles "+userProfiles.size());
Acquired userProfiles the size = 0.
Ah, and imagination are not the same.
Then looking, I noticed DevicePolicyManager also has a getBindDeviceAdminTargetUsers method that returns a List collection is a UserHandle.
DevicePolicyManager dpm = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
ComponentName mName = new ComponentName(this, TestMadmin.class);
List<UserHandle> listh = dpm.getBindDeviceAdminTargetUsers(mName );
Log.i("test","getBindDeviceAdminTargetUsers "+listh.size());
size = 1, and the user's current primary UserHandle. But my equipment obviously creates multiple users. Therefore, this method is also abandoned.
...
try to own a new UserHandle, you can get a UserHandle by UserHandle.getUserHandleForUid, but I do not know of other users uid. I tried to traverse 1 to 10 specimens to a UID other accounts. If I find the right one uid, then the multi-user should be able to be removed out.
ComponentName mName = new ComponentName(this, TestMadmin.class);
for(int i=1;i<=10;i++){
try {
UserHandle userHandle = UserHandle.getUserHandleForUid(i);
dpm.removeUser(mName ,userHandle );
} catch (Exception e) {
e.printStackTrace();
}
}
I created three users, successfully removed two other users. ! ? Half the battle.
The for loop is enlarged to 100. delete all. I can only create a maximum of three mobile phone users +1 visitors, a total of 4 users and 100 can delete perfect, even 2/30 probably enough.
UserHandle.getUserHandleForUid only api24 + only for api23 and below, you can call getUserHandleForUid inside out method call.
/**
* Returns the user for a given uid.
* @param uid A uid for an application running in a particular user.
* @return A {@link UserHandle} for that user.
*/
public static UserHandle getUserHandleForUid(int uid) {
return of(getUserId(uid));
}
/** @hide */
@SystemApi
public static UserHandle of(@UserIdInt int userId) {
return userId == USER_SYSTEM ? SYSTEM : new UserHandle(userId);
}
"Of" hide is to be obtained by reflecting
userHandle = (UserHandle) UserHandle.class.getMethod("of",int.class).invoke(null,i);
So, for equipment api23, deletion methods should be this:
ComponentName mName = new ComponentName(this, TestMadmin.class);
for(int i=1;i<=100;i++){
try {
userHandle = (UserHandle) UserHandle.class.getMethod("of",int.class).invoke(null,i);
dpm.removeUser(mName ,userHandle );
} catch (Exception e) {
e.printStackTrace();
}
}
For details on ComponentName and DevicePolicyManager, it will explain in Device Manager chapter.