java Android 权限。INTERACT_ACROSS_USERS 拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43556869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 07:32:18  来源:igfitidea点击:

Android permission.INTERACT_ACROSS_USERS denial

javaandroidandroid-permissions

提问by Michele Lacorte

I've a strange android permission denial, here is it:

我有一个奇怪的 android 权限拒绝,这里是:

java.lang.SecurityException: Permission Denial: isUserRunning() from pid=1078, uid=10284 requires android.permission.INTERACT_ACROSS_USERS

I haven't found anything about android.permission.INTERACT_ACROSS_USERSonly android.permission.INTERACT_ACROSS_USERS_FULL

我没有找到任何关于android.permission.INTERACT_ACROSS_USERSonlyandroid.permission.INTERACT_ACROSS_USERS_FULL

Here is the full logcat:

这是完整的日志:

java.lang.SecurityException: Permission Denial: isUserRunning() from pid=25403, uid=10310 requires android.permission.INTERACT_ACROSS_USERS
    at android.os.Parcel.readException(Parcel.java:1693)
    at android.os.Parcel.readException(Parcel.java:1646)
    at android.app.ActivityManagerProxy.isUserRunning(ActivityManagerNative.java:7000)
    at android.os.UserManager.isUserUnlocked(UserManager.java:1069)
    at android.os.UserManager.isUserUnlocked(UserManager.java:1063)
    at com.android.launcher3.compat.UserManagerCompatVN.isUserUnlocked(UserManagerCompatVN.java:39)
    at com.android.launcher3.LauncherModel$LoaderTask.loadWorkspace(LauncherModel.java:1759)
    at com.android.launcher3.LauncherModel$LoaderTask.loadAndBindWorkspace(LauncherModel.java:1387)
    at com.android.launcher3.LauncherModel$LoaderTask.run(LauncherModel.java:1486)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.os.HandlerThread.run(HandlerThread.java:61)

I've added this to my manifest:

我已将此添加到我的清单中:

<permission android:name="android.permission.INTERACT_ACROSS_USERS" android:protectionLevel="signature"/>
<permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL" android:protectionLevel="signature"/>

回答by Sina

TL;DR; Either this stack trace does not belong to your application or you need a permission that you don't have. To know about those permissions read the rest.

TL; 博士; 此堆栈跟踪不属于您的应用程序,或者您需要一个您没有的权限。要了解这些权限,请阅读其余部分。

Although Michele probably has found the answer, I've decided to answer this question as it might be useful for others. Mentioned permissions are signature|system level permissions. To read more about different types of permissions read this: Permissions overview

尽管 Michele 可能已经找到了答案,但我决定回答这个问题,因为它可能对其他人有用。提到的权限是签名|系统级权限。要阅读有关不同类型权限的更多信息,请阅读:权限概述

Basically these permissions are needed to use multi-user APIs such as:

基本上,使用多用户 API 需要这些权限,例如:

Context.startActivityAsUser(Intent, UserHandle)
Context.bindServiceAsUser(Intent, …, UserHandle)
Context.sendBroadcastAsUser(Intent, … , UserHandle)
Context.startServiceAsUser(Intent, …, UserHandle)

To know more, read this: Supporting Multiple Usersand this: Building Multiuser-Aware Apps

要了解更多信息,请阅读:支持多用户和此:构建多用户感知应用程序

Due to the error, Michele has come to this conclusion that he has to add these permissions to manifest (which we will see how it is possible for an application to have these permissions granted), but instead, he has defined these permissions(to know more about defining a permission read this: Define a Custom App Permission):

由于这个错误,Michele 得出了这个结论,他必须将这些权限添加到清单中(我们将看到应用程序如何被授予这些权限),但相反,他已经定义了这些权限(要知道有关定义权限的更多信息,请阅读:定义自定义应用程序权限):

<permission android:name="android.permission.INTERACT_ACROSS_USERS" android:protectionLevel="signature"/>
<permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL" android:protectionLevel="signature"/>

I think you will end up seeing a run time error because you can't define these permissions since they have the same name as two system permission that are already defined. Want to be sure? Take a look at a part of a real system manifest:

我认为您最终会看到运行时错误,因为您无法定义这些权限,因为它们与已定义的两个系统权限同名。想确定吗?看一下真实系统清单的一部分:

<!-- @SystemApi @hide Allows an application to call APIs that allow it to do interactions
     across the users on the device, using singleton services and
     user-targeted broadcasts.  This permission is not available to
     third party applications. -->
<permission android:name="android.permission.INTERACT_ACROSS_USERS"
    android:protectionLevel="signature|system|privileged" />

<!-- @hide Fuller form of {@link android.Manifest.permission#INTERACT_ACROSS_USERS}
     that removes restrictions on where broadcasts can be sent and allows other
     types of interactions. -->
<permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL"
    android:protectionLevel="signature" />

You see in this manifest file of an android system, these permissions are defined already and to use them we should use the tag.

你在这个 android 系统的清单文件中看到,这些权限已经定义,要使用它们,我们应该使用标签。

So now lets talk how theses permissions might be granted to your application. In my experience OEMs define system manifest in a way that these permissions could be granted to

那么现在让我们谈谈如何将这些权限授予您的应用程序。根据我的经验,OEM 以可以授予这些权限的方式定义系统清单

  1. Apps which have the same signature as the system(practically only apps which are developed by the OEM)

  2. Privileged apps being under the /system/priv-app.

  1. 与系统具有相同签名的应用程序(实际上只有 OEM 开发的应用程序)

  2. 特权应用程序位于 /system/priv-app 下。

In the system manifest I mentioned above the second permission is only defined as signature so only apps with the same signature as system can have those permissions granted.

在我上面提到的系统清单中,第二个权限仅定义为签名,因此只有与系统具有相同签名的应用程序才能授予这些权限。

If you have write access on a device (probably it should be rooted, I don't know much about that), you can copy your apk in the priv-app folder by this command:

如果您在设备上有写访问权限(可能它应该是 root 的,我对此知之甚少),您可以通过以下命令将您的 apk 复制到 priv-app 文件夹中:

adb push path-to-your-app/your-app.apk /system/priv-app

Is that all? Not yet!

这就是全部?还没有!

Since android 8.0 there are some complications about permissions being granted to applications under priv-app that you can read about it here: Privileged Permission Whitelisting

从 android 8.0 开始,将权限授予 priv-app 下的应用程序存在一些复杂性,您可以在此处阅读相关信息:Privileged Permission Whitelisting

回答by Gaurav Gupta

You should implement permission request at run-time in Android, specifically for Marshmallow or higher version. If you don't implement run-time permission, then your application will crash or will not work properly on the device having Marshmallow.

您应该在 Android 中在运行时实现权限请求,特别是对于 Marshmallow 或更高版本。如果您不实施运行时权限,那么您的应用程序将崩溃或无法在装有 Marshmallow 的设备上正常运行。

I hope you are now pretty much aware with Runtime Permission concept in Marshmallow. Let's understand this code:

我希望您现在已经非常了解 Marshmallow 中的运行时权限概念。让我们理解这段代码:

int currentAPIVersion = Build.VERSION.SDK_INT;
if(currentAPIVersion>=android.os.Build.VERSION_CODES.M)
 {
    if (ContextCompat.checkSelfPermission(context, Manifest.permission.INTERACT_ACROSS_USERS) != PackageManager.PERMISSION_GRANTED) {
        if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, Manifest.permission.INTERACT_ACROSS_USERS)) {
            AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
            alertBuilder.setCancelable(true);
            alertBuilder.setTitle("Permission necessary");
            alertBuilder.setMessage("Interact across users permission is necessary to this app");
            alertBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    ActivityCompat.requestPermissions((Activity)context, new String[]{Manifest.permission.INTERACT_ACROSS_USERS}, MY_PERMISSIONS_REQUEST_INTERACT_ACROSS_USERS);
                }
            });
            AlertDialog alert = alertBuilder.create();
            alert.show();
        } else {
            ActivityCompat.requestPermissions((Activity)context, new String[]{Manifest.permission.INTERACT_ACROSS_USERS}, MY_PERMISSIONS_REQUEST_INTERACT_ACROSS_USERS);
        }
    }
}


 @Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch (requestCode) {
 case MY_PERMISSIONS_REQUEST_INTERACT_ACROSS_USERS:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
               // do something here...
               Toast.makeText(getApplicationContext(), "Permission granted", Toast.LENGTH_SHORT).show();
        } else {
         //code for deny
         Toast.makeText(getApplicationContext(), "Permission denied", Toast.LENGTH_SHORT).show();

}
         break;
    }
}