Android 4.2&上面支持多个用户,如下面链接...
所述http://developer.android.com/about/versions/android-4.2.html#MultipleUsers
现在,我的问题是......
是否可以通过编程方式了解设备中配置的多个用户?如果是,那么如何获得设备中配置的用户数量?
是否可以获取用户列表?如果是,那么如何?
答案 0 :(得分:2)
有一个公共API可以获取用户详细信息,但仅限于拥有 import os
os.chdir('C:\\')
if os.path.exists('xxx.dat'):
airport = open('routes.dat')
for each_line in airport:
(Airlinecode,AirlineID,Source_airport_code,Source_airport_ID,Destination_airport,Destination_airport_ID,Codeshare,Stops,Equipment) = each_line.split("\t")
newlist =[]
newlist = Equipment
i = iter(newlist)
elements = []
for eachitem in range(len(newlist)):
elements.append(i.next())
print (elements)
airport.close()
else:
print('file does not exists')
权限的应用。此权限的保护级别为"android.permission.MANAGE_USERS"
,因此除非您的应用程序安装到系统分区,否则没有正式的方法来获取用户数。
我查看了源代码并提出了以下解决方案:
"signature|system"
它已经完成了我所做的简短测试,但它没有在任何生产代码中使用。它需要更多测试,但应该有效。
如果您的应用具有系统级权限,则会更容易:
public int getNumUsers() {
// The user directory created by android.server.pm.UserManagerService
File userDir = new File(Environment.getDataDirectory(), "user");
// Get the number of maximum users on this device.
int id = Resources.getSystem()
.getIdentifier("config_multiuserMaximumUsers", "integer", "android");
int maxUsers;
if (id != 0) {
maxUsers = Resources.getSystem().getInteger(id);
} else {
maxUsers = 5;
}
int numUsers = 0;
for (int i = 0; i <= maxUsers * 10; i += 10) {
// If a user is created, a directory will be created under /data/user in increments
// of 10. The first user will always be 0.
if (new File(userDir, Integer.toString(i)).exists()) {
numUsers++;
}
}
return numUsers;
}