无需询问存储权限

时间:2019-05-07 19:45:42

标签: android permissions storage

我已将这些权限添加到清单文件中

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

<uses-feature android:name="android.hardware.location.gps" />

<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

但是,当用户安装应用程序时,系统会询问他并允许他访问的唯一权限是位置

我还想问用户存储许可。这怎么可能?

3 个答案:

答案 0 :(得分:0)

使用运行时权限代码,在其中将数据添加到内部存储器中。您可以为此使用dexture或easy许可库。

答案 1 :(得分:0)

您是否尝试过Android文档指出的内容:

// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
        Manifest.permission.READ_CONTACTS)
        != PackageManager.PERMISSION_GRANTED) {

    // Permission is not granted
    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
            Manifest.permission.READ_CONTACTS)) {
        // 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; request the permission
        ActivityCompat.requestPermissions(thisActivity,
                new String[]{Manifest.permission.READ_CONTACTS},
                MY_PERMISSIONS_REQUEST_READ_CONTACTS);

        // 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
}

https://developer.android.com/training/permissions/requesting#java

答案 2 :(得分:0)

对于“危险”权限(包括存储,位置,摄像头,日历等),您需要在安装时(而不是在安装时)向用户请求其许可。 请务必在每次需要许可时进行检查,因为用户可以随时撤消该许可。

要检查权限(在Kotlin中)(在此示例中为FINE_LOCATION,请将其更改为您需要请求的任何权限):

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
        != PackageManager.PERMISSION_GRANTED
    ) {
//if permission not granted, start function to request permission
//MY_PERMISSIONS_REQUEST is the requestCode, an int > 0
        ActivityCompat.requestPermissions(
            this,
            arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
            MY_PERMISSIONS_REQUEST
        )
    }

,然后重写onRequestPermissionsResult函数:

    override fun onRequestPermissionsResult(
    requestCode: Int,
    permissions: Array<String>, grantResults: IntArray
) {
    when (requestCode) {
        MY_PERMISSIONS_REQUEST -> {
            // If request is cancelled, the result arrays are empty.
            if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
                //granted - you are not able to use Location Functionality

            } else {
                // permission denied
                Toast.makeText(this, "Location Functionality Disabled.", Toast.LENGTH_LONG).show()
            }
            return
        }

        // Add other 'when' lines to check for other
        // permissions this app might request.
        else -> {
            // Ignore all other requests.
        }
    }

}

您还可以一次请求多个权限。 Android 6.0 multiple permissions