如何区分 Android 中的主要和次要配置文件?

How to differentiate between primary and secondary profiles in Android?

我试图在我的次要个人资料上的应用 OTA 期间禁用一个组件。我正在编写一个接收器来通过捕获“android.intent.action.MY_PACKAGE_REPLACED”来禁用该组件。在此处添加我的 AndroidManifest.xml 更改,

<receiver android:name=".receivers.OTAUpdateReceiver"android:exported="true">
   <intent-filter>
      <action android:name="android.intent.action.MY_PACKAGE_REPLACED" /> 
   </intent-filter>
</receiver>

随着添加这些权限,

<uses-permission android:name="android.permission.INTERACT_ACROSS_USERS" />
<uses-permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL" />
<uses-permission android:name="android.permission.MANAGE_USERS" />
<uses-permission android:name="android.permission.CHANGE_COMPONENT_ENABLED_STATE" />

这是我的接收者代码,

private static final int PRIMARY_USER_ID = 0;
@Override
public void onReceive(Context context, Intent intent) {
    Log.i(TAG, "Entering here into ota update receiver");

    packageManager = context.getPackageManager();
    myComponent = new ComponentName(MY_PKG, MY_CLASS);

    if (mPackageManager.hasSystemFeature("com.abc.xyz") && !isPrimaryProfile()) {
        packageManager.setComponentEnabledSetting(
                myComponent,
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP);
    }
}
public boolean isPrimaryProfile() {

    int userId = ActivityManagerNative.getDefault().getCurrentUser().id;
    return (userId == PRIMARY_USER_ID) ? true : false;
}

我在收到此 MY_PACKAGE_REPLACED 意图时遇到以下运行时异常,

E/AndroidRuntime(13492): FATAL EXCEPTION: main E/AndroidRuntime(13492): Process: com.abc.project, PID: 13492 E/AndroidRuntime(13492): java.lang.RuntimeException: Unable to start receiver com.abc.project.receivers.OTAUpdateReceiver: java.lang.SecurityException: PermissionDenial: getCurrentUser() from pid=13492, uid=1032112 requires android.permission.INTERACT_ACROSS_USERS E/AndroidRuntime(13492): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2776) E/AndroidRuntime(13492): at android.app.ActivityThread.access00(ActivityThread.java:160) E/AndroidRuntime(13492): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1398) E/AndroidRuntime(13492): at android.os.Handler.dispatchMessage(Handler.java:102) E/AndroidRuntime(13492): at android.os.Looper.loop(Looper.java:135) E/AndroidRuntime(13492): at android.app.ActivityThread.main(ActivityThread.java:5597) E/AndroidRuntime(13492): at java.lang.reflect.Method.invoke(Native Method) E/AndroidRuntime(13492): at java.lang.reflect.Method.invoke(Method.java:372) E/AndroidRuntime(13492): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:984) E/AndroidRuntime(13492): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779) E/AndroidRuntime(13492): Caused by: java.lang.SecurityException: Permission Denial:getCurrentUser() from pid=13492, uid=1032112 requires android.permission.INTERACT_ACROSS_USERS E/AndroidRuntime(13492): at android.os.Parcel.readException(Parcel.java:1566) E/AndroidRuntime(13492): at android.os.Parcel.readException(Parcel.java:1519) E/AndroidRuntime(13492): at android.app.ActivityManagerProxy.getCurrentUser(ActivityManagerNative.java:4816) E/AndroidRuntime(13492): at

虽然此权限 "INTERACT_ACROSS_USERS" 已在 AndroidManifest.xml 中定义,但未得到反映。我错过了什么吗?或者有其他方法可以实现吗?

我的主要目标是区分主要个人资料和次要个人资料,或者确保用户不在主要个人资料中。

能够在 UserManager 的 getSerialNumberForUser api,

的帮助下区分主要和次要配置文件
public boolean isPrimaryProfile(final Context context) {
        UserHandle userHandle = Process.myUserHandle();
        UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
        return (userManager != null) ? userManager.getSerialNumberForUser(userHandle) == PRIMARY_USER_ID : false;
}

PRIMARY_USER_ID 的值对于主 user/owner 为 0。