I am using com.google.accompanist:accompanist-permissions:0.25.1 in my project. I am trying to request bluetooth permission in runtime which is requesting. I want to know how user know that permission is disable permanently.
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-feature android:name="android.hardware.bluetooth_le" android:required="false"/>
<!-- Request legacy Bluetooth permissions on older devices. -->
<uses-permission
android:name="android.permission.BLUETOOTH"
android:maxSdkVersion="30" />
<uses-permission
android:name="android.permission.BLUETOOTH_ADMIN"
android:maxSdkVersion="30" />
<uses-permission
android:name="android.permission.BLUETOOTH_SCAN"
android:usesPermissionFlags="neverForLocation"
tools:targetApi="s" />
<!-- Needed only if your app makes the device discoverable to Bluetooth devices. -->
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
<!-- Needed only if your app communicates with already-paired Bluetooth devices. -->
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-feature android:name="android.hardware.bluetooth" android:required="false"/>
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Permission"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.Permission">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
</application>
</manifest>
MainActivity.kt
package com.vivek.permission
import android.Manifest
import android.os.Build
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import com.google.accompanist.permissions.ExperimentalPermissionsApi
import com.google.accompanist.permissions.rememberMultiplePermissionsState
import com.vivek.permission.ui.theme.PermissionTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
PermissionTheme {
// A surface container using the 'background' color from the theme
Column(
modifier = Modifier
.fillMaxSize()
.background(MaterialTheme.colorScheme.background)
) {
ExampleScreenWithAccompanist()
}
}
}
}
}
val permissionsList = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
listOf(
Manifest.permission.BLUETOOTH,
Manifest.permission.BLUETOOTH_ADMIN,
Manifest.permission.BLUETOOTH_SCAN,
Manifest.permission.BLUETOOTH_CONNECT,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION
)
} else {
listOf(
Manifest.permission.BLUETOOTH,
Manifest.permission.BLUETOOTH_ADMIN,
Manifest.permission.ACCESS_COARSE_LOCATION,
)
}
@OptIn(ExperimentalPermissionsApi::class)
@Composable
fun ExampleScreenWithAccompanist() {
val multiplePermissionsState = rememberMultiplePermissionsState(permissions = permissionsList)
Text(text = "we need your permission", color = Color.White)
when {
multiplePermissionsState.allPermissionsGranted -> {
Text(text = " Permission Granted", color = Color.White)
}
multiplePermissionsState.shouldShowRationale -> {
Text(text = " Permission ShouldShowRationale", color = Color.White)
}
!multiplePermissionsState.allPermissionsGranted && !multiplePermissionsState.shouldShowRationale -> {
Text(
text = "Permission permanently denied ,you can enable it by going to app setting",
color = Color.White
)
}
}
Button(onClick = { multiplePermissionsState.launchMultiplePermissionRequest() }) {
Text(text = "Give permission", color = Color.White)
}
}
My 3 when statement run when user install first time the application. So how can i handle properly..
Try:
Explanation: In the above code, I have used the
registerForActivityResult()function to register the activity result. TheregisterForActivityResult()function takes two parameters, the first one is theActivityResultContracts.RequestMultiplePermissions()and the second one is the lambda function. The lambda function takes theMap<String, Boolean>as a parameter. TheMap<String, Boolean>contains the permission name as a key and the permission status as a value. The permission status is true if the permission is granted and false if the permission is denied.You can show the rational message when the user has denied the permission and the
shouldShowRequestPermissionRationale()function returns true. TheshouldShowRequestPermissionRationale()function returns true if the user has denied the permission and the user has not selected the “Don’t ask again” checkbox. The following code shows how to show the rational message.