I have this piece of code and I am not really sure if I should implement super.onRequestPermissionsResult(requestCode, permissions, grantResults) in the else block. I don't know why it should be needed.
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
when (requestCode) {
READ_EXTERNAL_STORAGE_REQUEST_CODE -> {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
performAction()
} else {
Toast.makeText(this, "You cancelled the permission", Toast.LENGTH_SHORT).show()
}
}
else -> super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
}
I have seen that there is an implementation for this method in the Activity. What does super.onRequestPermissionsResult(requestCode, permissions, grantResults) do? Is it needed?
currently it isn't necessary, as this method is empty. its only uppon to you for handling perm request result
but still you should keep super call in else statement - in this case and all other similar (overriden empty methods). you never know when Android team/library author introduce some default behavior/callback handle in source code (of Android, AndroidX, custom lib etc.) lib without using #CallSuper annotation
Related
I'm developing an APP using jetpack compose. On the onboarding screen I need to ask user for permission, but only when I ask for fine location permission, there is dialog popup. When I ask for BLUETOOTH, nothing happened. I debug my code, print log information.
Here is my button for requesting permission
Button(onClick = {
// navController.navigate(Screen.OnboardingLocationScreen.route)
Log.d("bluetooth permission", "${activity.checkSelfPermission(android.Manifest.permission.BLUETOOTH) == PackageManager.PERMISSION_GRANTED}")
ActivityCompat.requestPermissions(activity,
arrayOf(
android.Manifest.permission.BLUETOOTH,
android.Manifest.permission.BLUETOOTH_SCAN,
android.Manifest.permission.BLUETOOTH_CONNECT
),
3
)
}
It turns out without asking permission, Bluetooth is already granted. I find log information is true and I receive message from onRequestPermissionsResult() without grant permission on the app.
#Deprecated("Deprecated in Java")
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == 3) {
Log.d("permission","bluetooth")
}
Here is log information
D/bluetooth permission: true
D/permission: bluetooth
I'm using API29 now.
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
// super.onRequestPermissionsResult(requestCode, permissions, grantResults)
Log.wtf("here grand result" , requestCode.toString(),)
Log.wtf("here grand result" , grantResults.toString(),)
if(requestCode == PERMISSION_REQUEST_ACCESS_LOCATION){
if(grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED){
Toast.makeText(requireActivity(),"permission granted",Toast.LENGTH_SHORT).show()
getUserLocation()
}else{
Toast.makeText(requireActivity(),"Denied",Toast.LENGTH_SHORT).show()
}
}
}
I am always get the auto toast message from here but when location granted then i wants to call a function but this override method not working inside fragment please help me ?
Thanks in advance
Use below code to call onRequestPermissionResult method in fragment
requestPermisions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION, LOCATION_PERMISSION_REQUEST_CODE}
Don't use ActivityCompat.requestPermission because it should called from activity only not in fragment.
I am new to Android App development and I'd really like to know if there is a way to check in another Class (a Foreground Service that gathers some location data) if the location Permission was given in the Main Activity.
In my main Activity, I am requesting the permission straight upon app start like this:
private fun requestPermissions() {
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION),
PERMISSION_ID
)
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == PERMISSION_ID) {
if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
Toast.makeText(this, "Right Permissions Granted", Toast.LENGTH_LONG).show()
}
}
}
}
Its working and I can give my app the permission to access the location. To use a function in my other I class, I need to check if the permission was granted, and I do it like this:
fun dummy(){
if (ActivityCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_FINE_LOCATION
) == PackageManager.PERMISSION_GRANTED
) {
// do work that needs the location permission
}
}
However, if I try to execute this function, I get a null pointer reference. What am I missing here?
Thank you!
You can't request permissions from a Service. The reason is that when the request permission dialog comes up, the user is naturally going to think it belongs to the foreground app. THat confusion is why Google doesn't allow it. My best suggestion would be to launch an Activity that then asks for permission.
I want onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) method in fragment
I found google and anwser fragment
(mainActivity)getActivity.onrequestPermissionResult
but I don't know this parametor
Do you have any other way or know this parameter? All I know is requestCode
viewmodel
private val _permissionVisibility = MutableLiveData<Boolean>(ContextCompat.checkSelfPermission(context, Manifest.permission.READ_CONTACTS) != PERMISSION_GRANTED)
val permissionVisibility: LiveData<Boolean>
get() = _permissionVisibility
xml binding
android:visibility="#{homeViewModel.permissionVisibility ? View.VISIBLE : View.GONE}"
mainActivity
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
Timber.d("Test ${requestCode} , ${permissions} , $grantResults ")
}
You can use new Activity Result APIs to get the result of permission and activityresult in much simplier way.
The Activity Results API provides two methods — RequestPermission and RequestMultiplePermissions . These two does exactly what their names are. Here’s a quick example code.
// Requesting Location Permission
bi.btnRequestPermission.setOnClickListener {
askLocationPermission(android.Manifest.permission.ACCESS_FINE_LOCATION)
}
// Single Permission Contract
private val askLocationPermission = registerForActivityResult(ActivityResultContracts.RequestPermission()) { result ->
if(result){
Log.e("TAG", "Location permnission granted")
}else{
Log.e("TAG", "Location permnission denied")
}
}
// -------------------------------------------------------------------
// Requesting Mutliple Permissions - Location & Bluetooth
bi.btnRequestPermission.setOnClickListener {
askMultiplePermissions(arrayOf(
android.Manifest.permission.ACCESS_FINE_LOCATION,
android.Manifest.permission.BLUETOOTH
))
}
you have to add the following dependencies in your app’s build.gradle file.
implementation 'androidx.fragment:fragment-ktx:1.3.0-alpha04'
Check the blog for reference- https://wajahatkarim.com/2020/05/activity-results-api-onactivityresult/
I need to use the camera on one of my pages, but it won't work (NotAllowedError: Permission denied), and I tried to authorise the camera on my app via my device but it's still not wot working
I also added the authorisations on my Manifest:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera.any" android:required="false" />
But I think it's because I use jasonnette that the app use the authorisation of the navigator I display rather than the authorisations of the app itself
You have to ask for the runtime permissions I did it this way in a project some time ago:
First:
ActivityCompat.requestPermissions(activity!!,
arrayOf(android.Manifest.permission.CAMERA),
PERMISSIONS_REQUEST_CAMERA)
Then:
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
when (requestCode) {
PERMISSIONS_REQUEST_CAMERA -> {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//do your stuff
}
return
}
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
I hope this helps!