我正在尝试设置一种在活动初始化后注册权限结果处理程序的方法,方法是创建一个
Map<Integer, Runnable>
来保存处理程序,当我请求权限时,我使用随机生成的代码并将可运行对象保存到 Map代码/处理程序。
这是我目前在活动课上的内容
private final HashMap<Integer, Runnable> onPermission = new HashMap<>();
//i call this from other classes to ask for a permission and register a handler
public void requestPermission(Runnable onGranted, String...permissions) {
int code = Permissions.permissionRequestCode();
onPermission.put(code, onGranted);
requestPermissions(permissions, code);
}
//overriding this method to check if the Map has a handler for the granted request
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
Runnable handler = onPermission.get(requestCode);
if (handler != null && isGranted(grantResults)) {
handler.run();
onPermission.remove(requestCode);
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
//utility method to check if all the requested permissions are granted
private boolean isGranted(int[] results) {
for (int i : results) {
if (i != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
}
现在在实际设备(Android 12,API 31)上运行它,就像我期望的那样工作(请求显示,并且在授予权限时执行处理程序),但是在模拟器(Android 13,API 33)上,当请求权限时没有任何显示,并且它仍然是“未授予”,权限甚至不显示在“应用程序信息”中,即使它包含在清单中。
真实设备:
模拟器:
我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="Mesa"
android:roundIcon="@mipmap/ic_launcher"
android:supportsRtl="true"
android:theme="@style/Theme.Mesa.NoActionBar"
android:usesCleartextTraffic="true">
<activity
android:name=".app.Mesa"
android:configChanges="uiMode|screenSize|colorMode"
android:exported="true"
android:theme="@style/Theme.Mesa.NoActionBar"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我的build.gradle:
android {
compileSdk 33
defaultConfig {
applicationId "org.luke.mesa"
minSdk 24
targetSdk 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
proguardFiles 'proguard-rules.pro'
}
buildTypes {
release {
minifyEnabled true
}
debug {
minifyEnabled false
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
allprojects {
tasks.withType(JavaCompile){
options.compilerArgs <<"-Xlint:deprecation"
}
}
}
buildFeatures {
viewBinding true
dataBinding true
}
packagingOptions {
resources {
merges += ['META-INF/DEPENDENCIES']
}
}
namespace 'org.luke.mesa'
dependenciesInfo {
includeInApk true
includeInBundle true
}
buildToolsVersion '33.0.0'
}
可能的重复并没有解决我的问题: