When I overriding function onRequestPermissionsResult is not working.
When I remove the override command Android Studio don't complain anymore but the function remains useless...
I'm requesting permissions for ACCES_FINE_LOCATION
The error message is
Modifier 'override' is not applicable to 'local function'
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
val texto = TextView(this)
val ID_REQUISICAO_ACCESS_FINE = 1
setContentView(texto)
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
when (requestCode) {
ID_REQUISICAO_ACCESS_FINE -> {
// If request is cancelled, the result arrays are empty.
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the location-related task you need to do.
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "permission granted", Toast.LENGTH_LONG).show()
}
} else {
// permission denied, boo! Disable the functionality that depends on this permission.
Toast.makeText(this, "permission denied", Toast.LENGTH_LONG).show()
}
return
}
}
}
}
}
You have onRequestPermissionsResult as overriden method of class, not local function of onCreate method. So, just close onCreate method before starting onRequestPermissionsResult one:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
val texto = TextView(this)
val ID_REQUISICAO_ACCESS_FINE = 1
setContentView(texto)
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
when (requestCode) {
ID_REQUISICAO_ACCESS_FINE -> {
// If request is cancelled, the result arrays are empty.
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the location-related task you need to do.
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "permission granted", Toast.LENGTH_LONG).show()
} else {
// permission denied, boo! Disable the functionality that depends on this permission.
Toast.makeText(this, "permission denied", Toast.LENGTH_LONG).show()
}
return
}
}
}
}
}
Related
I would like to create a custom ActivityResultContract to request both coarse and fine location that has a custom response.
class LocationPermission : ActivityResultContract<Void?, LocationPermissionResult>() {
override fun createIntent(context: Context, input: Array<String>): Intent {
val requestPermissions = arrayOf(
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
)
return Intent(ActivityResultContracts.RequestMultiplePermissions.ACTION_REQUEST_PERMISSIONS)
.putExtra(ActivityResultContracts.RequestMultiplePermissions.EXTRA_PERMISSIONS, requestPermissions)
}
...
}
Calling that from an activity:
private val reportLocationIntent = registerForActivityResult(LocationPermission()) { result ->
}
...
reportLocationIntent.launch()
However when doing so createIntent is never called. What am I doing wrong?
Your code in question has compilation errors, however this approach seems to work. I don't really know what the "custom response" should be from the question directly, so I've created a class called class LocationPermissionResult(val granted: Boolean) based on your question. It is mostly compiled from RequestMultiplePermissions from ActivityResultContracts.
Tested on API 30 and API 33 emulator, the permission dialog opens and the returned result is correct as well. (Make sure the permissions are added to AndroidManifest.xml)
Here is the source code:
class LocationPermission : ActivityResultContract<Void?, LocationPermissionResult>() {
private val permissions = arrayOf(
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
)
override fun createIntent(context: Context, input: Void?): Intent {
return Intent(ActivityResultContracts.RequestMultiplePermissions.ACTION_REQUEST_PERMISSIONS).putExtra(
ActivityResultContracts.RequestMultiplePermissions.EXTRA_PERMISSIONS, permissions)
}
override fun getSynchronousResult(context: Context,
input: Void?): SynchronousResult<LocationPermissionResult>? {
val allGranted = permissions.all { permission ->
ContextCompat.checkSelfPermission(
context,
permission
) == PackageManager.PERMISSION_GRANTED
}
return if (allGranted) {
SynchronousResult(LocationPermissionResult(true))
} else null
}
override fun parseResult(resultCode: Int, intent: Intent?): LocationPermissionResult {
if (resultCode != Activity.RESULT_OK) return LocationPermissionResult(false)
if (intent == null) return LocationPermissionResult(false)
val grantResults = intent.getIntArrayExtra(
ActivityResultContracts.RequestMultiplePermissions.EXTRA_PERMISSION_GRANT_RESULTS)
return LocationPermissionResult(grantResults?.all { it == PackageManager.PERMISSION_GRANTED } == true)
}
}
class LocationPermissionResult(val granted: Boolean)
this is my solution to request both coarse and fine location, i hope this will do the job
object Permission {
private const val REQUEST_CODE_PERMISSION: Int = 1
#SuppressLint("ObsoleteSdkInt")
fun isGrantedLocationPermission(context: Context): Boolean {
return if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
true
} else {
context.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED ||
context.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED
}
}
fun grantLocationPermission(context: Context) {
val permissions = arrayOf(
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
)
(context as MainActivity).requestPermissions(permissions, REQUEST_CODE_PERMISSION)
}
}
Use it from MainActivity:
override fun onCreate(savedInstanceState: Bundle?) {
...
val button = findViewById<Button>(R.id.btnGetLocation)
button.setOnClickListener {
getLocation()
}
}
private fun getLocation() {
//check if permission isnt granted:
if (!Permission.isGrantedLocationPermission(this))
Permission.grantLocationPermission(this)
else {
...
}
}
//Check if user give permission or not:
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//permission granted
} else {
//permission denied
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
I want to make a phone call using the number displayed on the view in the recycler view.
override fun onBindViewHolder(holder: MyViewholder, position: Int) {
val currentItem = userlist[position]
holder.txtFirstName.text = currentItem.firstName
holder.txtLastName.text = currentItem.lastName
holder.txtAge.text = currentItem.phonenumber.toString()
num = currentItem.phonenumber.toString()
holder.call.setOnClickListener {
val number: String = currentItem.phonenumber.toString()
if (ContextCompat.checkSelfPermission(
context,
android.Manifest.permission.CALL_PHONE
) != PERMISSION_GRANTED
) {
ActivityCompat.requestPermissions(
context as Activity,
arrayOf(android.Manifest.permission.CALL_PHONE),
REQUEST_CALL
)
} else {
val dial = "tel:$number"
val intent = Intent(Intent.ACTION_CALL, Uri.parse(dial))
context.startActivity(intent)
}
}
}
I am not able to override OnResquestPermissionResult() inside the adapter.
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
if (requestCode == REQUEST_CALL) {
if (grantResults.size > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
val number: String = num
if (ContextCompat.checkSelfPermission(
context,
android.Manifest.permission.CALL_PHONE
) != PERMISSION_GRANTED
) {
ActivityCompat.requestPermissions(
context as Activity, arrayOf(android.Manifest.permission.CALL_PHONE),
REQUEST_CALL
)
} else {
onRequestPermissionsResult(requestCode,permissions,grantResults)
val dial = "tel:$number"
val intent = Intent(Intent.ACTION_CALL, Uri.parse(dial))
context.startActivity(intent)
}
} else {
Toast.makeText(context, "Permission Denied", Toast.LENGTH_SHORT).show()
}
}
}
if I try to override OnResquestPermissionResult() inside the activity I am not able to pass the current phone number from the view.
I removed this whole code:
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
if (requestCode == REQUEST_CALL) {
if (grantResults.size > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
val number: String = num
if (ContextCompat.checkSelfPermission(
context,
android.Manifest.permission.CALL_PHONE
) != PERMISSION_GRANTED
) {
ActivityCompat.requestPermissions(
context as Activity, arrayOf(android.Manifest.permission.CALL_PHONE),
REQUEST_CALL
)
} else {
onRequestPermissionsResult(requestCode,permissions,grantResults)
val dial = "tel:$number"
val intent = Intent(Intent.ACTION_CALL, Uri.parse(dial))
context.startActivity(intent)
}
} else {
Toast.makeText(context, "Permission Denied", Toast.LENGTH_SHORT).show()
}
}
}
and everything is working fine.
Dont do that, return your click call back into your activity or fragment, then do your request permission like this
Request permission in android
or this, new way of get permission
Request permission in android with activity result api
WhoDoctor was helped me on the Previous code
Now, the android application crashed when during running on my phone
and This is the error log
First problem is after I scanned the QRcode with camera it cannot show at the QRcode's result into the tvResult
Second problem is after I selected a QRcode image from the storage, then tap confirmed, it crash
Here below I think are the issue
Type mismatch: inferred type is Uri? but Uri was expected
galleryLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult(), object :ActivityResultCallback<ActivityResult>{
override fun onActivityResult(result: ActivityResult?) {
val data=result?.data
inputImage = InputImage.fromFilePath(requireContext(), data?.data)
processQr()
}
})
Redundant SAM-constructor
binding.btnScanBarcode.setOnClickListener{
val options=arrayOf("camera","gallery")
val builder=AlertDialog.Builder(requireContext())
builder.setTitle("Pick a option")
builder.setItems(options, DialogInterface.OnClickListener { dialog, which ->
if(which==0){
val cameraIntent=Intent(MediaStore.ACTION_IMAGE_CAPTURE)
cameraLauncher.launch(cameraIntent)
}else{
val storageIntent=Intent()
storageIntent.setType("image/*")
storageIntent.setAction(Intent.ACTION_GET_CONTENT)
galleryLauncher.launch(storageIntent)
}
})
builder.show()
}
'onRequestPermissionsResult(Int, Array<(out) String!>, IntArray): Unit' is deprecated. Deprecated in Java
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if(requestCode==CAMERA_PERMISSION_CODE){
if(grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED){
checkPermission(
Manifest.permission.READ_EXTERNAL_STORAGE,
READ_STORAGE_PERMISSION_CODE)
}else{
Toast.makeText(requireContext(), "Camera Permission Denied", Toast.LENGTH_SHORT).show()
}
}else if(requestCode==READ_STORAGE_PERMISSION_CODE){
if((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)){
checkPermission(
Manifest.permission.WRITE_EXTERNAL_STORAGE,
WRITE_STORAGE_PERMISSION_CODE)
}else{
Toast.makeText(requireContext(), "Storage Permission Denied", Toast.LENGTH_SHORT).show()
}
}else if(requestCode==WRITE_STORAGE_PERMISSION_CODE) {
if (!(grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
Toast.makeText(requireContext(), "Storage Permission Denied", Toast.LENGTH_SHORT).show()
}
}
}
onRequestPermissionsResult() method is deprecated in androidx.fragment.app.Fragment.
So you use registerForActivityResult() method instead onRequestPermissionsResult().
Following is kotlin code. .
val permReqLuncher = registerForActivityResult(ActivityResultContracts.RequestPermission()){
if (it) {
// Good pass
} else {
// Failed pass
}
}
How to get a permission request in new ActivityResult API (1.3.0-alpha05)?
private ActivityResultLauncher<String> mPermissionResult = registerForActivityResult(
new ActivityResultContracts.RequestPermission(),
new ActivityResultCallback<Boolean>() {
#Override
public void onActivityResult(Boolean result) {
if(result) {
Log.e(TAG, "onActivityResult: PERMISSION GRANTED");
} else {
Log.e(TAG, "onActivityResult: PERMISSION DENIED");
}
}
});
// Launch the permission window -- this is in onCreateView()
floatingActionButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mPermissionResult.launch(Manifest.permission.ACCESS_BACKGROUND_LOCATION);
}
});
You can request multiple permissions.
val requestMultiplePermissions = registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { permissions ->
permissions.entries.forEach {
Log.e("DEBUG", "${it.key} = ${it.value}")
}
}
requestMultiplePermissions.launch(
arrayOf(
Manifest.permission.READ_CONTACTS,
Manifest.permission.ACCESS_FINE_LOCATION
)
)
So here I tried implementing the above functionality but only managed to fetch image files only through the gallery,fileExplorer and google photos, but I'm not able to find out a way to do the same for a third party library like the camscanner or officeLens
private val IMAGE_PICK_CODE = 1000;
private val PERMISSION_CODE = 1001;
fab.setOnClickListener { view ->
if (VERSION.SDK_INT >= VERSION_CODES.M){
if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) ==
PackageManager.PERMISSION_DENIED){
val permissions = arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE);
requestPermissions(permissions, PERMISSION_CODE);
}
else{
pickImageFromGallery();
}
}
else{
pickImageFromGallery();
}
}
}
private fun pickImageFromGallery() {
val intent = Intent(Intent.ACTION_PICK)
intent.type = "image/*"
startActivityForResult(intent, IMAGE_PICK_CODE)
}
//
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
when(requestCode){
PERMISSION_CODE -> {
if (grantResults.size >0 && grantResults[0] ==
PackageManager.PERMISSION_GRANTED){
//permission from popup granted
pickImageFromGallery()
}
else{
//permission from popup denied
Toast.makeText(this, "Permission denied", Toast.LENGTH_SHORT).show()
}
}
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (resultCode == Activity.RESULT_OK && requestCode == IMAGE_PICK_CODE){
imageView.setImageURI(data?.data)
}
}
manifest file
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA"/>
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.
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.
}
});
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.
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
}