简体   繁体   English

如何确定哪个用户安装了android应用程序?

[英]How to determine which user installed the android application?

With Android 4.2 and above in tablets, multiple users are supported. 使用Android 4.2及更高版本的平板电脑,可支持多个用户。 It means that each user can have his own separate set of applications which will be kept separate from other users. 这意味着每个用户都可以拥有自己独立的应用程序集,这些应用程序将与其他用户分开。 Also if User A installs an application, it will not be available when User B logs in. 此外,如果用户A安装了应用程序,则在用户B登录时将无法使用该应用程序。

So my question is, if there is a way to determine using PackageManager when an application gets installed, can I know which user actually installed it? 所以我的问题是,如果在安装应用程序时有一种方法可以确定使用PackageManager ,我可以知道哪个用户实际安装了它吗?

Unfortunately, there is not a simple and straightforward way to 'identify' which user installed your app. 不幸的是,没有一种简单直接的方法可以“识别”哪个用户安装了您的应用。 The only alternative is to associate a user with a unique Id as stated in the documentation : 唯一的选择是将用户与文档中所述的唯一ID相关联:

By creating a new UUID when your app starts for the first time, you're certain to obtain a unique ID for tracking each user, regardless of how many users install your app on a single device. 通过在您的应用首次启动时创建新的UUID,无论用户在单个设备上安装了多少用户,您都可以获得用于跟踪每个用户的唯一ID。

Therefore, although you may not be able to retrieve a user directly by some sort of credential, you can simulate your own with( code here ): 因此,虽然您可能无法通过某种凭证直接检索用户,但您可以使用( 此处的代码 )模拟您自己的用户:

public class Installation {
    private static String sID = null;
    private static final String INSTALLATION = "INSTALLATION";

    public synchronized static String id(Context context) {
        if (sID == null) {  
            File installation = new File(context.getFilesDir(), INSTALLATION);
            try {
                if (!installation.exists())
                    writeInstallationFile(installation);
                sID = readInstallationFile(installation);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return sID;
    }

    private static String readInstallationFile(File installation) throws IOException {
        RandomAccessFile f = new RandomAccessFile(installation, "r");
        byte[] bytes = new byte[(int) f.length()];
        f.readFully(bytes);
        f.close();
        return new String(bytes);
    }

    private static void writeInstallationFile(File installation) throws IOException {
        FileOutputStream out = new FileOutputStream(installation);
        String id = UUID.randomUUID().toString();
        out.write(id.getBytes());
        out.close();
    }
}

I found the solution. 我找到了解决方案。 The best way to get user information is using UserManager API. 获取用户信息的最佳方法是使用UserManager API。 Though some API's require the application to be a system app, we can get all information about the current user name, user id etc. 虽然某些API要求应用程序成为系统应用程序,但我们可以获取有关当前用户名,用户ID等的所有信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何确定哪个用户正在从Android应用程序发出Rest请求? - How to determine which user is making Rest requests from android application? 确定应用程序是否已安装在android上 - Determine if application is installed on android 如何确定用户正在使用的应用程序(在android中)? - How to determine that an application in being used by the user (in android)? 如何在Android中过滤用户安装的应用程序? - How to filter user installed application in Android? 如何创建可以确定用户何时离开家的 android 应用程序? - How can I create an android application which can determine when a user has left their house? 确定Android中已安装应用程序使用的权限列表 - Determine list of permissions used by an installed application in Android 如何学习以编程方式在Android中安装了哪个版本的应用程序 - How to learn which version of application installed programmatically in android Android如何知道已安装我的应用程序的联系人? - Android How to know which Contacts have my application installed? 如何确定哪个服务器与Android中的用户更近/更快? - How to determine which server is closer/faster to the user in Android? 如何测试Facebook用户是否已使用sdk 3安装了我的android应用程序 - How to test if a Facebook user has installed my android application with sdk 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM