请求权限在android 13或12中无效

wixjitnu  于 2022-11-27  发布在  Android
关注(0)|答案(2)|浏览(1156)

我将我的android项目从compileSdkVersion 31更新为compileSdkVersion 33。我有下一个代码来启动我在应用中需要的权限,但它不起作用

import android.Manifest
import android.app.Activity
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import android.os.Build


class Permissions(private val activity: Activity, private val permissionList: List<String>, val code: Int) {

    fun checkPermissions() {
        if (permissionsGranted() != PackageManager.PERMISSION_GRANTED) {
            requestPermissions()
        }
    }

    private fun permissionsGranted(): Int {
        var counter = 0
        for (permission in permissionList) {
            counter += ContextCompat.checkSelfPermission(activity, permission)
        }
        return counter
    }

    private fun deniedPermission(): String {
        for (permission in permissionList) {
            if (ContextCompat.checkSelfPermission(activity, permission) == PackageManager.PERMISSION_DENIED) return permission
        }
        return ""
    }

    private fun requestPermissions() {
        val permission = deniedPermission()
        if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permission)) {

        } else {

            ActivityCompat.requestPermissions(activity, permissionList.toTypedArray(), code)
        }
    }

    fun checkPermission(manifest: String): Boolean {
        val result = ContextCompat.checkSelfPermission(activity, manifest)
        return result == PackageManager.PERMISSION_GRANTED
    }

    fun checkPermissionActivity(state: Boolean, urlPath: String, kind : String){
        val showRationale = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            this.activity.shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE) && this.activity.shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)
        } else {
            true
        }

        if (!showRationale) {

            return
        } else {

            return
        }
    }

    fun getAllPermissions(): List<String> {
        val granted = ArrayList<String>()
        val pi = activity.packageManager.getPackageInfo(activity.packageName, PackageManager.GET_PERMISSIONS)
        for (i in pi.requestedPermissions.indices) {
            if (pi.requestedPermissionsFlags[i] and PackageInfo.REQUESTED_PERMISSION_GRANTED != 0) {
                granted.add(pi.requestedPermissions[i])
            }
        }

        return granted
    }

}

清单是

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.co.retrofit.app">
    <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CAMERA" />
    <application
            android:name=".feature.RetrofitApplication"
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:networkSecurityConfig="@xml/network_security_config"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme"
            android:usesCleartextTraffic="true">
        <activity
                android:exported="true"
                android:name=".feature.view.activities.Maintenance" />
        <activity android:name=".feature.view.activities.SplashActivity"  android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".feature.view.activities.MainActivity"  android:exported="true"/>
    </application>

</manifest>

当我单击一个按钮时调用函数

@Suppress("UNUSED_PARAMETER")
private fun addAlbum(view: View){
    val permissions = Permissions(this,
        arrayListOf(
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE),
        23)
    permissions.checkPermissions()
}

@Suppress("UNUSED_PARAMETER")
private fun addAlbum(view: View){
    val permissions = Permissions(this,
        arrayListOf(
            Manifest.permission.CAMERA),
        23)
    permissions.checkPermissions()
}

如果我回滚更新后的文件,它会显示我需要的权限,但我需要33中的编译

现在,如果我只更新编译SDK,它不会显示弹出窗口

2izufjch

2izufjch1#

针对Android 13,您需要在manifest中声明您想要读取的媒体类型。您不能直接在manifest中请求READ_EXTERNAL_STORAGE
假设你想要一个音频许可,那么READ_MEDIA_AUDIO,对于视频许可READ_MEDIA_VIDEO,对于图像READ_MEDIA_IMAGES等等。
此外,如果您正在迁移到Android 13,那么this article可以提供很大帮助。

bqujaahr

bqujaahr2#

Android 13中,Google引入了粒度存储权限,这意味着您现在必须指定要在应用中使用的媒体类型。
引入了3种类型的权限:
READ_MEDIA_AUDIOREAD_MEDIA_VIDEOREAD_MEDIA_IMAGES中的一个或多个。
U在Android 13上不能再请求READ_EXTERNAL_STORAGE权限了,我说的不再是指没有人能够确定原因。一旦U编译为sdk 33,它就停止在运行时请求,并从应用设置中删除权限。
有关详细信息,请参阅:https://developer.android.com/about/versions/13/behavior-changes-13#granular-media-permissions

相关问题