Reputation: 488
My application only shows permission dialog only first time after app has been installed. If I close it and open again, it doesn't show request window. Even if I manually disable location permission or request permission window on clickEvent it doesnt work.
checkPermission:
private fun checkPermission(){
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
//permission not granted
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.ACCESS_COARSE_LOCATION)) {
//show an explanation to the user
} else {
//request the permission
ActivityCompat.requestPermissions(this,
arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION),
ACCESS_COARSE_LOCATION_CODE)
}
} else {
ActivityCompat.requestPermissions(this,
arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION),
ACCESS_COARSE_LOCATION_CODE)
}
}
onRequestPermissionsResult:
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
when(requestCode){
ACCESS_COARSE_LOCATION_CODE -> {
if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
//granted
} else {
//denied
}
return
}
}
}
I call checkPermission
function on onStart
and when I click icon.
Upvotes: 0
Views: 2487
Reputation: 322
private fun checkAndRequestPermissions(): Boolean {
val permissionContact = ContextCompat.checkSelfPermission(activity,
Manifest.permission.READ_CONTACTS)
val permissionPhoneState = ContextCompat.checkSelfPermission(activity,
Manifest.permission.READ_PHONE_STATE)
val permissionCamera = ContextCompat.checkSelfPermission(activity,
Manifest.permission.CAMERA)
val permissionWriteExternal = ContextCompat.checkSelfPermission(activity,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
val locationPermission = ContextCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION)
val listPermissionsNeeded = java.util.ArrayList<String>()
when {
locationPermission != PackageManager.PERMISSION_GRANTED -> listPermissionsNeeded.add(Manifest.permission.ACCESS_FINE_LOCATION)
}
when {
permissionContact != PackageManager.PERMISSION_GRANTED -> listPermissionsNeeded.add(Manifest.permission.READ_CONTACTS)
}
when {
permissionPhoneState != PackageManager.PERMISSION_GRANTED -> listPermissionsNeeded.add(Manifest.permission.READ_PHONE_STATE)
}
when {
permissionCamera != PackageManager.PERMISSION_GRANTED -> listPermissionsNeeded.add(Manifest.permission.CAMERA)
}
when {
permissionWriteExternal != PackageManager.PERMISSION_GRANTED -> listPermissionsNeeded.add(Manifest.permission.WRITE_EXTERNAL_STORAGE)
}
when {
!listPermissionsNeeded.isEmpty() -> ActivityCompat.requestPermissions(activity, listPermissionsNeeded.toTypedArray(), REQUEST_ID_MULTIPLE_PERMISSIONS)
}
return true
}
Upvotes: 0
Reputation: 456
when the user accepts your permission it accepts it permanently (unless the user remove it) check permission shows only when the permission is not granted yet. when the user clear data or uncheck the permission through setings->app settings-> chosen app->permisson.
Upvotes: 0
Reputation: 23414
you left shouldShowRequestPermissionRationale
part empty so it is not requesting permission second time . In it show an alert dialog why you need permission and when user clicks Ok ask for permission again . As an alternate you can ask permission again directly.
private fun checkPermission(){
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
//permission not granted
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.ACCESS_COARSE_LOCATION)) {
ActivityCompat.requestPermissions(this,
arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION),
ACCESS_COARSE_LOCATION_CODE)
} else {
//request the permission
ActivityCompat.requestPermissions(this,
arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION),
ACCESS_COARSE_LOCATION_CODE)
}
} else {
ActivityCompat.requestPermissions(this,
arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION),
ACCESS_COARSE_LOCATION_CODE)
}
}
You are not handling the deny and dont ask again cases properly , I would suggest you to use this library for simplicity . you will get all call backs at single place
Permissions.check(this, Manifest.permission.ACCESS_COARSE_LOCATION, null, new PermissionHandler() {
@Override
public void onGranted() {
// do your task.
}
});
Upvotes: 3