【问题标题】:What is bindServiceAsUser() method in Android?什么是 Android 中的 bindServiceAsUser() 方法?
【发布时间】:2015-06-03 08:37:08
【问题描述】:

我无法理解 bindServiceAsUser() 方法的用途。任何人都可以请解释一下吗?谷歌搜索似乎没有多大帮助。

    public boolean bindService(Intent intent, ServiceConnection connection, int flags) {
    return mContext.bindServiceAsUser(intent, connection, flags, UserHandle.OWNER);
}

【问题讨论】:

    标签: android android-context bindservice


    【解决方案1】:

    我从来没有觉得有必要使用bindServiceAsUser(),但以下是 Android 文档对此的说明:

    与 bindService(android.content.Intent,android.content.ServiceConnection,int) 相同,但有一个明确的 userHandle 参数供系统服务器和其他多用户感知代码使用。

    Android 4.2 (API: 17) 中添加了多用户支持,请阅读HERE。据我了解,它将主要由设备制造商使用,例如为企业世界发布特殊设备。我发现的针对多用户的最佳文档是THIS one,以及那里的所有引用链接。

    【讨论】:

    • 个人开发者什么时候应该使用bindServiceAsUser()
    【解决方案2】:

    正如 Vesko 所说,在大多数 android 设备中,多用户被禁用。一些设备制造商启用它。例如,您必须将服务与 AIDl 绑定并在您的特权应用程序中为用户禁用功能。在这里你需要知道绑定服务是哪个用户。我们可以使用反射调用bindServiceAsUser

      UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
            UserHandle owner = null;
                        owner = um.getUserForSerialNumber(0L);
                      try {
                MethodUtils.invokeMethod(getApplicationContext(), "bindServiceAsUser", new Object[]{i, serviceConnection, Context.BIND_AUTO_CREATE, owner});
            } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                e.printStackTrace();
            }
    

    【讨论】:

      猜你喜欢
      • 2013-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-25
      • 1970-01-01
      • 1970-01-01
      • 2012-05-01
      • 2020-04-19
      相关资源
      最近更新 更多