【问题标题】:Permission not asked未请求许可
【发布时间】:2018-12-04 13:53:26
【问题描述】:

我想知道为什么在启动我的应用程序时没有询问我的权限,这是我的清单权限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

当我通过手机的参数时,我只有“位置”权限(并且它被禁用)。

【问题讨论】:

  • 您的设备和目标sdk是否至少为26?因为 Marshmallow 引入了运行时权限。
  • 我有一个 Xperia XA1 Ultra (Android 8.0) 和 SDK 28(我在我的 gradle 中看到了,对吧?)

标签: android permissions manifest


【解决方案1】:

您应该请求运行时权限 请参阅文档: Request App Permissions

【讨论】:

    【解决方案2】:

    如果未询问您的权限是指未提示用户允许 Internet 权限,则这是正常的。 Internet 在正常权限列表中,因此它是自动授予的。有关正常权限的更多信息,请查看:https://developer.android.com/guide/topics/permissions/normal-permissions.html

    此外,添加权限是一个两步过程;一旦您在清单中声明了所需的权限,您还必须在您的 java 文件中进行一些设置。看看https://developer.android.com/training/permissions/requesting

    此外,如果您正在寻找更简单的方法来处理权限,那么也有一些库可以解决这个问题,例如 RxPermissions:https://github.com/tbruyelle/RxPermissions

    希望这会有所帮助!

    【讨论】:

    • 是的,我是这个意思。感谢您的信息,我会尽快检查。
    【解决方案3】:

    如果您的目标是 SDK 26+,那么您需要检查代码中的权限,如下所示:

    if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.ACCESS_FINE_LOCATION)
        != PackageManager.PERMISSION_GRANTED) {
    // Permission is not granted
    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
            Manifest.permission.ACCESS_FINE_LOCATION)) {
        // Show an explanation to the user *asynchronously* -- don't block
        // this thread waiting for the user's response! After the user
        // sees the explanation, try again to request the permission.
    } else {
        // No explanation needed, we can request the permission.
        ActivityCompat.requestPermissions(thisActivity,
                arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
                MY_PERMISSIONS_ACCESS_FINE_LOCATION)
    
        // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
        // app-defined int constant. The callback method gets the
        // result of the request.
    }
    } else {
    // Permission has already been granted - or running on old Android
    }
    

    Request App Permissions中有描述

    【讨论】:

      【解决方案4】:

      对于 marshmallow(API 23) 及更高版本,您应该获得位置、电话状态和其他dangerous permissions 的权限,不仅在您的清单中,而且在您的代码中(运行时权限)。对于其他权限,清单就足够了。

      this video 你也可以在this Q&A找到你的解决方案

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-02-03
        • 1970-01-01
        • 1970-01-01
        • 2019-05-01
        • 1970-01-01
        • 2018-07-27
        相关资源
        最近更新 更多