Android 多用户

    在Android 4.2中加入了多用户的支持,但是只在平板端适用,主要是因为在使用Android平板的时候,像邮件、即时聊天这些信息会被平板所接收,这没什么问题,切换了用户也就切换了这些网络工具的账号,推送是到达正确的方面。而手机则不同,手机上会接收到来电、私人短信,并且不管现在登录的账号是哪个,现在这个问题google也没有一个明确的解决方向。

    多用户并不是什么新概念,类似windows,linux等桌面操作系统都有这样的功能,但是android多用户还是有自己的一些特性,首先,第一个使用平板的用户默认为平板管理员,可以在平板设置选项的用户区添加或删除帐号。网络设置和安装应用程序的权限在平板所有用户中通用和共享,但墙纸、主屏幕、锁屏布局、PIN码、屏幕明亮度、单个应用设置等则各不相同。跟Windows操作系统的不同之处在于,4.2系统的其他用户帐户中正在运行的后台程序也会占用一定的RAM空间。而最明显的区别是用户之间的应用程序无法共享,这样一来想要用没付费的帐户在同一台机器下载其他账户已经付费的内容时,需要在商店用已付费的帐户密码重新登录,如果想要保护隐私的话就没办法跟其他用户共享付费软件。(多个账户重复下载同一应用时不会在存储空间里出现相同的两个应用,而是进行虚拟下载和安装,不用担心占用空间。)用户之间不能直接共享数据,即使将平板和电脑相连,也只能浏览当前用户的文件,切换帐号需要重新装载,虽说有些麻烦但是安全性得到了充分保证。

    多用户的支持判断:系统判断当前设备是否支持多用户模式的依据是配置文件config.xml中的config_multiuserMaximumUsers配置项。 其取值为整型,决定着当前设备支持的最大用户上限。默认值为1,即不支持多用户。代码的判断在UserManager.java:

    /**
     * Returns the maximum number of users that can be created on this device. A return value
     * of 1 means that it is a single user device.
     * @hide
     * @return a value greater than or equal to 1
     */
    public static int getMaxSupportedUsers() {
        // Don't allow multiple users on certain builds
        if (android.os.Build.ID.startsWith("JVP")) return 1;
        return SystemProperties.getInt("fw.max_users",
                Resources.getSystem().getInteger(R.integer.config_multiuserMaximumUsers));
    }

     多用户的相关操作:对用户的操作目前未对普通应用开放,其相关API都有hide注解,并需要system权限。此外,用户的添加和移除还要需添加android.Manifest.permission.MANAGE_USERS权限。

<!-- @hide Allows an application to call APIs that allow it to query and manage
         users on the device. This permission is not available to
         third party applications. -->
    <permission android:name="android.permission.MANAGE_USERS"
        android:permissionGroup="android.permission-group.SYSTEM_TOOLS"
        android:protectionLevel="signature|system"
        android:label="@string/permlab_manageUsers"
        android:description="@string/permdesc_manageUsers" />

    添加用户:用户添加是通过调用UserManager(系统服务)的public UserInfo createUser(String name, int flags)方法进行的。其具体实现在UserManagerService的同名方法中。

    保存用户:用户创建后,会首先在/data/system/users/userlist.xml文件中保存新增加用户的id,创建/data/system/users/ 用户id 目录,并将用户信息保存至其下的用户id.xml 文件中。其内容包括一些基本的用户信息。

    用户切换:用户切换是通过调用ActivityManager的public boolean switchUser(int userId)方法进行。

    用户移除:用户移除是通过调用UserManager的 public boolean removeUser(int userHandle) 方法进行的。

    ActivityManagerService目前加入了多用户支持。负责维护设备中存在的所有用户状态。服务以下述变量来记录当 前处于“启动”状态的用户。

/** 
     * Which uses have been started, so are allowed to run code. 
     */  
    final SparseArray mStartedUsers = new SparseArray();  
   
    /** 
     * LRU list of history of current users.  Most recently current is at the end. 
     */  
    final ArrayList mUserLru = new ArrayList();  
   
    /** 
     * Constant array of the users that are currently started. 
     */  
    int[] mStartedUserArray = new int[] { 0 }; 

用户的启动状态对象为com.android.server.am.UserStartedState。其中指定的用户状态有四种:

  • public final static int STATE_BOOTING = 0; //用户启动
  • public final static int STATE_RUNNING = 1; //运行中
  • public final static int STATE_STOPPING = 2; //停止中
  • public final static int STATE_SHUTDOWN = 3; //用户关闭状态

完整的用户生命周期为:
BOOTING->RUNNING->STOPPING->SHUTDOWN

未完待续!

猜你喜欢

转载自yangmengli.iteye.com/blog/2058849