Flutter - Android doesn't ask for permissions - android

I'm trying to get the current location of the device using the plugin location but Android never asks for permissions. I get this error:
Exception has occurred. PlatformException
(PlatformException(PERMISSION_DENIED_NEVER_ASK, Location permission
denied forever- please open app settings, null))
I have added the line below in every `AndroidManifest.xml´ file that I could find in the Android folder (no idea which one I should use actually, I found 3 of them)
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
I tried the plugin permission_handler to request them manually but still, no pop-up.
I'm completely lost and I find nothing similar on the net. Here is the code :
Future<LocationData> _getLocation() async {
LocationData currentLocation;
// returns a map with unreadable numbers which make no sense
var t = await PermissionHandler()
.requestPermissions([PermissionGroup.location]);
// returns false
var test = await location.requestPermission();
try {
currentLocation = await location.getLocation();
} catch (e) {
currentLocation = null;
}
return currentLocation;
}
Edit: I tried this on my device (OnePlus 6) and on an emulator (Pixel XL API 28). I've also tried to uninstall/reinstall the app.

After asking on the git of location plugin, I did this :
flutter clean
And I got the pop up asking for permissions.
Thank to this guy.

You can handle this kind of permission behavior in your code. Even if you never had installed your app on emulator this behavior can happen and you must handle this in your code because this can happen when your app are in final user hands.
This exception means that you're getting PermissionStatus.denied from checkPermissionStatus permission handler plugin method and this can happen when user hits Never ask again checkbox in android permission dialog and deny the request.
What is the most simple way to handle this?
If you get PermissionStatus.denied from checkPermissionStatus method you can show a dialog telling to the user that your app needs of that permission to provide a better experience and in this dialog you can redirect the user to Android permission settings where the user can enable the requested permission manually.
You can do this using openAppSettings() method from permission handler plugin.
Maybe you should read this article, it's a little bit old but it shows the correct flow about how to ask and handle user permissions in android. This article doesn't talks about the new permissions changes in newer Android Q.

The error message tries to make it clear: Your device (you or someone else on the device you are testing on) has selected Never when Android asked for the permission previously.
This means that Android will not ask for the permission again.
Just try it on a different device or uninstall the app again.

add this
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

Related

ACCESS_MEDIA_LOCATION permission request is not showing prompt

I've set my app to target AP 29 and removed requestLegacyExternalStorage=true from manifest.
Now I'm checking if the user has this permission and if result is denied I request for permission.
My problem is that the request for permission is returning Granted without showing the prompt... I know the flow is working since I'm able to read the GPS location from picture after being granted.
I see permission status = Denied and as soon as I explicitly request this permission, it returns Granted without any user interaction.
Eveything looks OK but I'm confused about not seeing the prompt... is this expected? I saw this permission qualifies as "Dangerous" so I was expecting a prompt. I'm testing on a Android 10 device.
I'm not showing any code since the project is Xamarin and the permission logic is handled through a third party library, don't think my code will help as the platform logic to request the permission is hidden by the component.
From
Android 10: fetch the gallery via MediaStore with location information :
This requires holding the ACCESS_MEDIA_LOCATION permission. Note, this permission is not "user visible in the settings UI" (source), which means the user won't see a popup asking for permission, even though it is a runtime permission. This means you have to ask for permission during runtime (in contrast to just the manifest file) but the user won't have to consent to it. Adding this here because you might be wondering why no extra UI popups are shown.
I'm still getting my head around the logic though. I'm in favour of the user being asked for permission but I don't understand why it should be necessary to "request" it if the user doesn't actually grant permission.
I was able to reproduce the issue in a simpler app. I have posted a slightly different question with code snippets.
This is an answer by HilaryN that I believe should not have been deleted (I removed the off-topic bits).

ARCore App terminates after revoking camera permissions from android settings

Like the title of my question already tells:
I got an android app which makes use of ARCore.
I make also use of ARCores Sceneform.
Everything works fine but when I tested my app I went to the android settings while my ARCore app was running in the background, revoked the camera permissions and my app immediately stopped running without any error message.
In the Android Studio log I only get the message 'Application terminated'.
On my phone the app just closes without any message.
How can I prevent this?
I got this error with ARCore 1.3 and 1.4.
In your AndroidManifest.xml probably you has this line <uses-feature android:name="android.hardware.camera.ar" android:required="true"/> put as false
Then on your onResume activity you need to create a function to check the permissions, ask for permissions if don't has and deal with the exception when the user don't give or remove the permissions.
More info how check, ask and handl permissions:
https://developer.android.com/training/permissions/requesting#java

Setting a mock location programmatically in FusedLocationProviderClient

I am trying to use mock locations in my app for testing. To receive location updates I used the latest android developer documentation here:
https://developer.android.com/training/location/receive-location-updates
This tutorial uses FusedLocationProviderClient and locationClient.requestLocationUpdates(createLocationRequest(), locationCallback, null) as example.
The issue is that I can no longer use permission:
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
because I get the error "Permission is only granted to system apps" (I tried also in debug mode)
So I tried the following code, but it doesn't work:
locationClient.setMockMode(true);
String[] tokens = location.split(",");
Location newLocation = new Location(LocationManager.GPS_PROVIDER);
newLocation.setLatitude(Float.parseFloat(tokens[0]));
newLocation.setLongitude(Float.parseFloat(tokens[1]));
newLocation.setAccuracy(3.0f);
newLocation.setTime(System.currentTimeMillis());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
newLocation.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
}
locationClient.setMockLocation(newLocation);
Does anyone have an idea how to solve this?
It seems that adding the
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
permission to debug android manifest works even though Android Studio says "Permission is only granted to system apps".
So the solution is that, after adding it, run the application, then go to Settings / Developer options / Select mock location app -> here my app appears and I can select it.
I hope others can confirm and use this!

Ionic Android permission always returns false

I am using ionic 3.15 and trying to use the permissions plugin.
Using the code in the docs as mentioned here.
I have the code as shown in the docs but will mention it again here.
this.androidPermissions.checkPermission(this.androidPermissions.PERMISSION.CAMERA).then(
success => console.log('Permission granted'),
err => this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.CAMERA)
);
I always get hasPermission: false.
I do not get any prompt for turning the permissions on.
What is to be done here I am clueless.
Thanks.
You need to add something along the lines of:
this.androidPermissions.requestPermissions(
[
this.androidPermissions.PERMISSION.READ_EXTERNAL_STORAGE,
this.androidPermissions.PERMISSION.WRITE_EXTERNAL_STORAGE
]
);
You check to see if you have permissions, if it is false, then execute the above code to prompt the user to allow.
It seems like you have to make sure that you are using the permission you requested. Otherwise no prompt will be shown and the androidPermissions.requestPermissions will immediately return false.
From the docs:
Android 26 and above: due to Android 26's changes to permissions handling (permissions are requested at time of use rather than at runtime,) if your app does not include any functions (eg. other Ionic Native plugins) that utilize a particular permission, then requestPermission() and requestPermissions() will resolve immediately with no prompt shown to the user. Thus, you must include a function utilizing the feature you would like to use before requesting permission for it.

Does Eclipse automatically set "uses-permissions" in Manifest?

I know it's a simple question but I can't find any answer. Well actually it's three related questions:
If my code requires a uses-permission manifest element, does Eclipse automatically add it to the manifest?
If Eclipse doesn't automatically add it, how do I know which permissions my app needs? Of course there is this list, but it's hard to go though this list checking if what my app does falls within each of these permissions.
If Eclipse doesn't automatically add the permission and I fail to do it, how will I find out? Will the app fail to install on the emulator? Will it install on the emulator but be force-closed when trying to access something it doesn't have permissions for? Or do I have to install the apk on a real device in order to find out?
If my code requires a uses-permission manifest element, does Eclipse automatically add it to the manifest?
No.
how do I know which permissions my app needs?
Generally, by reading the JavaDocs, which do a decent job of pointing out what permissions you need. Otherwise, you will find out in testing, when your app crashes with a SecurityException.
If Eclipse doesn't automatically add the permission and I fail to do it, how will I find out?
See above.
Will it install on the emulator but be force-closed when trying to access something it doesn't have permissions for?
Correct.
Eclipse will not add permissions automatically. However, if you try to use a feature that requires permission, you will be made aware of the missing permission. Here's an excerpt from android resource page on Permissions: Link
Often times a permission failure will result in a SecurityException
being thrown back to the application. However, this is not guaranteed
to occur everywhere. For example, the sendBroadcast(Intent) method
checks permissions as data is being delivered to each receiver, after
the method call has returned, so you will not receive an exception if
there are permission failures. In almost all cases, however, a
permission failure will be printed to the system log.
Your third question is answered by:
In almost all cases, however, a permission failure will be printed to
the system log.
Just in case you're wondering about what you would see in Logcat:
11-20 08:08:47.766: E/AndroidRuntime(9380):
java.lang.SecurityException: Need BLUETOOTH permission: Neither user
10111 nor current process has android.permission.BLUETOOTH.
Eclipse does not automatically add the uses-permission to your manifest. I once had forgot to add a permission and had my app fail when it got to that part of the code. I can't remember the exact error but it did mention that a permission was required to use the method I tried using and I believe that it told me what permission.
If you don't add one in then you will soon find out.

Categories

Resources