我将我的安卓项目从compileSdkVersion 31更新到compileSdkVersion 33。我有以下代码来启动应用程序中需要的权限,但它不起作用。
该文本的意思是:清单是。
当我点击一个按钮时,我调用这个函数。
如果我回滚更新,它会显示我需要的权限,但是我需要在33中编译。 现在,如果我只更新编译 SDK,它不会显示弹出窗口。
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,它不会显示弹出窗口。