UserManager getUserCount() (Jelly Bean)

 ̄綄美尐妖づ 提交于 2019-11-28 05:18:24

问题


I am working on this class : UserManager

public int getUserCount ()

Return the number of users currently created on the device.

My code is:

UserManager um = (UserManager) getSystemService(USER_SERVICE);
int count = um.getUserCount();
Log.i("count",""+count);

It produce error like this:

 Caused by: java.lang.SecurityException: You need MANAGE_USERS permission to: query users
    at android.os.Parcel.readException(Parcel.java:1425)
    at android.os.Parcel.readException(Parcel.java:1379)
    at android.os.IUserManager$Stub$Proxy.getUsers(IUserManager.java:321)
    at android.os.UserManager.getUsers(UserManager.java:198)
    at android.os.UserManager.getUserCount(UserManager.java:186)
    at com.example.multiusertest.MainActivity.onCreate(MainActivity.java:52)
    at android.app.Activity.performCreate(Activity.java:5104)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)

I already added this permission manually in Manifest file. Does anyone know why it is produce.

Manifest File:

<uses-sdk
    android:minSdkVersion="17"
    android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.MANAGE_USERS"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.multiusertest.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>


回答1:


The MANAGE_USERS has a protectionlevel of signature|system, which means that the application has to be signed with the platform key. See this thread on XDA




回答2:


You can get the same result but from querying the profiles:

UserManager um = (UserManager) getSystemService(USER_SERVICE);
int count = um.getUserProfiles().size();

That way you can count the number of the returned users handles.



来源:https://stackoverflow.com/questions/13508258/usermanager-getusercount-jelly-bean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!