如何获得用户的列表用于当前装置中,即使棒棒糖前? [英] How to get a list of users for the current device, even before Lollipop?

查看:395
本文介绍了如何获得用户的列表用于当前装置中,即使棒棒糖前?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

由于API 17(果冻豆),有可能为Android用户必须使用同一设备的多个用户。在开始的时候仅是为平板电脑,但棒棒堂(API 21)是适用于智能手机了。

我想检查哪些应用程序当前用户具​​有跻身当前设备的其他用户共享,这样我就可以知道用户是否应被通知可以卸载所有用户来完成(写< A HREF =htt​​p://stackoverflow.com/q/28739533/878126> 这里 )。那是因为我做了可以管理和卸载应用的应用程序( 这里 )。

问题

我无法找到一个方法来做到这一点很好,甚至没有用root。

我已经试过

我想我已经找到一种方法来做到这一点,通过使用 < STRONG>的UserManager 软件包管理系统 这样:

 的UserManager微米=(的UserManager)getSystemService(Context.USER_SERVICE);
最终的软件包管理系统软件包管理系统= getPackageManager();
Log.d(APPLOG,用户数:+ um.getUserCount());
为(UserHandle userHandle:um.getUserProfiles()){
  最终长serialNumberForUser = um.getSerialNumberForUser(userHandle);
  Log.d(APPLOG,序列号:+ serialNumberForUser);
  最终的String [] = packagesForUid packageManager.getPackagesForUid((INT)serialNumberForUser);
  StringBuilder的SB =新的StringBuilder();
  对于(字符串的packageName:packagesForUid){
    sb.append(+的packageName);
  }
  Log.d(APPLOG,套餐+ sb.toString());
}

但是,它有一些问题:
- 功能 getUserProfiles 要求的API 21以上。
- 功能<一个href=\"http://developer.android.com/reference/android/content/pm/PackageManager.html#getPackagesForUid(int)\"相对=nofollow> getPackagesForUid 需要一个INT参数,而我有一个长变量(从<一href=\"http://developer.android.com/reference/android/os/UserManager.html#getSerialNumberForUser(android.os.UserHandle)\"相对=nofollow> getSerialNumberForUser )。不知道这是真正的正确方法。
- 我已经对的UserManager级使用的功能最多(?或全部)需要一个许可MANAGE_USERS,但显然这是一个签名|系统的权限,这是不给那么容易。

所以它不工作,和崩溃。

问题

反正有没有找到这个信息?其中的应用程序被安装为其他用户呢?


解决方案

如果你有root访问权限,而Android不会再次改变目录结构,可以列举 /数据/用户的内容/ 。每个子目录重新presents用户,而且每个二级子目录再presents一个安装的应用程序,例如 /data/user/0/com.example.maypp /data/user/1/com.example.someotherapp <​​/ code >。

Background

Since API 17 (Jelly Bean), it's possible for Android users to have multiple-users using the same device. In the beginning it was only for tablets, but on Lollipop (API 21) it's available for smartphones too.

I'm trying to check out which apps the current user have that are shared amongst other users of the current device, so that I could know if the user should be notified that uninstallation can be done for all users (written about here). That's because I've made an app that can manage and uninstall apps (here).

The problem

I can't find a way to do it nicely, not even using root.

What I've tried

I thought I've found a way to do it, by using "UserManager" and "PackageManager" as such:

UserManager um=(UserManager)getSystemService(Context.USER_SERVICE);
final PackageManager packageManager=getPackageManager();
Log.d("AppLog","users count:"+um.getUserCount());
for(UserHandle userHandle : um.getUserProfiles()){
  final long serialNumberForUser=um.getSerialNumberForUser(userHandle);
  Log.d("AppLog","serial number:"+serialNumberForUser);
  final String[] packagesForUid=packageManager.getPackagesForUid((int)serialNumberForUser);
  StringBuilder sb=new StringBuilder();
  for(String packageName : packagesForUid){
    sb.append(packageName+" ");
  }
  Log.d("AppLog","packages:"+sb.toString());
}

However, it has some issues: - the function "getUserProfiles" requires API 21 and above. - the function "getPackagesForUid" requires an "int" parameter, while I have a "long" variable (from "getSerialNumberForUser"). Not sure if that's really the correct way. - Most (or all?) of the functions I've used on the "UserManager" class require a permission "MANAGE_USERS" , but apparently this is a "signature|system" permission, which isn't given so easily.

So it doesn't work, and crashes.

The question

Is there anyway to find this information? of which apps are installed for other users too?

解决方案

If you have root access, and Android doesn't change the directory structure again, you can enumerate the contents of /data/user/. Each subdirectory represents a user, and each second-level subdirectory represents an installed app, e.g. /data/user/0/com.example.maypp or /data/user/1/com.example.someotherapp.

这篇关于如何获得用户的列表用于当前装置中,即使棒棒糖前?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆