[英]Android permission request always denied
我正在使用 Android Studio 中的 Java 構建一個 Android 應用程序,該應用程序需要藍牙權限才能運行。
我的問題是,每次我請求所需的權限時,都會自動拒絕我的請求。 此外,發出請求后不會出現權限對話框。
我請求的權限是BLUETOOTH_ADVERTISE
, BLUETOOTH_CONNECT
, BLUETOOTH
。 這是清單文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp">
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-feature
android:name="android.hardware.bluetooth_le"
android:required="true" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApp">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
這是build.gradle
文件。 我不知道它是否重要,但我將其包括在內只是為了確定。
plugins {
id 'com.android.application'
}
android {
compileSdk 31
defaultConfig {
applicationId "com.example.myapp"
minSdk 23
targetSdk 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'),
'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.4.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
implementation "androidx.fragment:fragment:1.4.0"
implementation "androidx.activity:activity:1.4.0"
testImplementation 'junit:junit:'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
這是檢查並請求許可的代碼:
/*
Returns true if all the permissions are already granted.
Returns false if there are missing permissions.
*/
@RequiresApi(api = Build.VERSION_CODES.S)
private boolean checkPermissions() {
System.out.println("Checking permissions");
ArrayList<String> permissions = new ArrayList<>();
if (ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_ADVERTISE)
== PackageManager.PERMISSION_DENIED) {
permissions.add(Manifest.permission.BLUETOOTH_ADVERTISE);
}
if (ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH)
== PackageManager.PERMISSION_DENIED) {
permissions.add(Manifest.permission.BLUETOOTH);
}
if (ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT)
== PackageManager.PERMISSION_DENIED) {
permissions.add(Manifest.permission.BLUETOOTH_CONNECT);
}
if (permissions.size() != 0) {
System.out.println("Missing the following permissions: " + permissions.toString());
requestPermissionLauncher.launch(permissions.toArray(new String[0]));
//ActivityCompat.requestPermissions(MainActivity.this, permissions.toArray(new String[0]), 0);
return false;
}
return true;
}
這是應該請求許可的請求許可啟動器:
@RequiresApi(api = Build.VERSION_CODES.S)
private final ActivityResultLauncher<String[]> requestPermissionLauncher =
registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), results -> {
// Ensure all permissions have been granted
boolean isGranted = true;
for (boolean entry : results.values()) {
if (!entry) {
isGranted = false;
break;
}
}
if (isGranted) {
Toast.makeText(getApplicationContext(), "Permissions Granted", Toast.LENGTH_SHORT).show();
System.out.println("Permission granted");
} else {
Toast.makeText(getApplicationContext(), "Permissions Denied", Toast.LENGTH_SHORT).show();
System.out.println("Permission denied");
Toast.makeText(this, "You need to grant all permissions to use this application", Toast.LENGTH_LONG).show();
}
});
幾天來我一直試圖找到解決方案,但沒有任何成功。 我讀過關於即時應用程序無法訪問確定權限的信息,但我認為這不是我的情況:我不是在構建即時應用程序。
此問題是否與我的 Android 設備和 API 版本有關? 我的設備是運行 Android 版本 8.0.0 (Oreo) 的三星 Galaxy A5 2017。
謝謝你的時間。
這個問題是我的一個愚蠢的錯誤。 我在測試 Android 設備上使用了不可用的權限。
我通過使用BLUETOOTH_ADMIN
權限而不是BLUETOOTH_ADVERTISE
和BLUETOOTH_CONNECT
解決了這個問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.