[英]Android: ActivityCompat.requestPermissions does not show a popup (Android 13, targetSdkVersion=33)
EDITS BELOW =)以下编辑 =)
There are many ma ny ma ny questions on this topic on SO already, I know.我知道,关于这个话题已经有很多很多问题了。 However, none of the answers I have found so far resolves the issue for me.
但是,到目前为止,我找到的所有答案都无法为我解决问题。
The problem: That the ActivityCompat.requestPermissions
does not trigger a popup, asking the user to give permission for notifications.问题:
ActivityCompat.requestPermissions
不会触发弹出窗口,要求用户授予通知权限。
Setup:设置:
<uses-permission android:name="android.permission.POST_NOTIFICATION" />
<uses-permission android:name="android.permission.POST_NOTIFICATION" />
if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_DENIED)
{
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.POST_NOTIFICATIONS}, 1);
}
and the callback is like so:回调是这样的:
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 1:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainScreenActivity.this, "Woho, you have enabled notifications!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainScreenActivity.this, "Ouch, this is gonna hurt without notifications", Toast.LENGTH_SHORT).show();
}
return;
}
}
Here are the build.gradle files:以下是 build.gradle 个文件:
TheApp\build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
maven { url 'https://raw.github.com/xujiaao/mvn-repository/master/releases' }
maven {
url 'https://jitpack.io'
}
maven { url 'http://dl.bintray.com/ahmedrizwan/maven'
allowInsecureProtocol = true
}
google()
}
dependencies {
classpath 'com.google.gms:google-services:4.3.10' // Google Services plugin
classpath 'com.google.firebase:firebase-crashlytics-gradle:2.5.2'
classpath 'com.android.tools.build:gradle:7.3.1'
classpath "io.realm:realm-gradle-plugin:10.0.0"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
maven { url 'http://oss.jfrog.org/artifactory/oss-snapshot-local'
allowInsecureProtocol = true
}
google()
jcenter()
flatDir {
dirs 'src/main/libs'
}
}
}
TheApp\app\build.gradle
apply plugin: 'com.android.application'
apply plugin: 'realm-android'
apply plugin: 'com.google.firebase.crashlytics'
android {
compileSdkVersion 33
lintOptions {
abortOnError false
}
buildFeatures{
dataBinding = true
}
defaultConfig {
applicationId "test.myapp"
minSdkVersion 29
targetSdkVersion 33
versionCode 60
versionName "1.60"
}
buildTypes {
debug {
signingConfig signingConfigs.debug_keystore
aaptOptions.setProperty("cruncherEnabled", false)
}
release {
signingConfig signingConfigs.release_keystore
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
aaptOptions.setProperty("cruncherEnabled", false)
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
namespace 'test.myapp'
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'com.google.firebase:firebase-core:17.5.1'
implementation 'com.google.firebase:firebase-messaging:20.3.0'
implementation 'com.google.code.gson:gson:2.8.6'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'com.google.android.gms:play-services-gcm:17.0.0'
implementation 'com.google.android.gms:play-services-location:17.1.0'
implementation 'com.google.android.gms:play-services-maps:17.0.0'
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
implementation "androidx.preference:preference-ktx:1.1.1"
implementation 'com.google.firebase:firebase-crashlytics:17.2.2'
implementation 'com.google.firebase:firebase-analytics:17.6.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.2'
implementation 'com.google.android.play:app-update:2.0.1'
def fragment_version = "1.5.4"
// Java language implementation
implementation "androidx.fragment:fragment:$fragment_version"
// Kotlin
implementation "androidx.fragment:fragment-ktx:$fragment_version"
// Testing Fragments in Isolation
debugImplementation "androidx.fragment:fragment-testing:$fragment_version"
}
apply plugin: 'com.google.gms.google-services' // Google Play services Gradle plugin
I have stepped through the code via the debugger, and can confirm that the requestPermissions
is executed, but that the onRequestPermissionsResult
is triggered immediately with no popup on device.我已经通过调试器单步执行了代码,并且可以确认
requestPermissions
已执行,但onRequestPermissionsResult
立即触发,设备上没有弹出窗口。
I also read this , but I figured that it was down to developers not implementing the opt-in model as described here , but maybe, this is an issue after all?我也读过这篇文章,但我认为这是由于开发人员没有按照 此处所述实施选择加入 model,但也许这毕竟是一个问题? Cause I cannot make sense of this...
因为我无法理解这个...
I ask for other permissions in other parts of the app, and this line works well - I get the popup to grant permission:我在应用程序的其他部分请求其他权限,这一行效果很好——我得到了授予权限的弹出窗口:
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, BaseActivity.PHONE_STATE_PERMISSION_CODE);
In another part of the code base, I have the same requestPermission call, but for another permission, and this works like a charm:在代码库的另一部分,我有相同的 requestPermission 调用,但需要另一个权限,这就像一个魅力:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, BaseActivity.PHONE_STATE_PERMISSION_CODE);
}
When this is executed, I get this:执行此操作时,我得到以下信息:
But, if I change the requestPermissions call so it looks like this, nothing happens and I get to the callback immediately, with a "rejected":但是,如果我更改 requestPermissions 调用,使其看起来像这样,什么也不会发生,我会立即进入回调,并收到“拒绝”消息:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.POST_NOTIFICATIONS}, BaseActivity.PHONE_STATE_PERMISSION_CODE);
}
I have also tried using the new ActivityResultLauncher
, but the behaviour is the same - it goes directly to the callback without showing any popup.我也尝试过使用新的
ActivityResultLauncher
,但行为是一样的——它直接进入回调而不显示任何弹出窗口。
update your AndroidX dependency, as only newest ones will have code handling freshly introduced permission.更新您的 AndroidX 依赖项,因为只有最新的依赖项才会拥有新引入的代码处理权限。
replace代替
implementation 'androidx.appcompat:appcompat:1.2.0'
with和
implementation 'androidx.appcompat:appcompat:1.5.1'
generally try to update (and consolidate) AndroidX libs version通常尝试更新(和整合)AndroidX 库版本
Stupid me.愚蠢的我。 In the Q above, I did post what the AndroidManifest contained, and I wrote:
在上面的 Q 中,我确实发布了 AndroidManifest 包含的内容,并且我写道:
<uses-permission android:name="android.permission.POST_NOTIFICATION" />
This is not correct.这是不正确的。 I am missing an
s
, it shoul be:我缺少一个
s
,它应该是:
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
Oops: :-)糟糕::-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.