-1

So I've been trying to get started on a simple GPS application but at some point I'm required to ask for user permission on locationManager methods

if (isNetworkEnabled) {
                    locationManager.requestLocationUpdates(
                            locationManager.NETWORK_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                if (locationManager != null) {
                    location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        Latitude = location.getLatitude();
                        Longitude = location.getLongitude();
                    } //location
                } //locationManager
            } //isNetworkEnabled

I understand that there's a simple way to do it for API 23, but I'd like to target Android 4.0.

I've seen a documentation about it (https://developer.android.com/training/permissions/requesting.html) but I just don't quite understand how to apply it to my code. I know I'm supposed to use ActivityCompact and ContextCompact but I'm just not sure on how. Please help!

MuriloRM
  • 59
  • 1
  • 11
  • 4
    runtime premissions are only used for API > 23. For API< 23 premissions are granted while installing application – Vygintas B Jan 02 '17 at 14:47
  • @VygintasB so in this case how is user permission asked for? – MuriloRM Jan 02 '17 at 14:48
  • They are asked when the user clicks on `Install` on the Play Store. – Prerak Sola Jan 02 '17 at 14:52
  • 1
    If you're not targeting API> 23 you only need to write permission inside manifest – Vygintas B Jan 02 '17 at 14:53
  • Runtime permissions are marshmellow thing – Vygintas B Jan 02 '17 at 14:53
  • @PrerakSola I understand that's usually the case, but when I'm accessing locations through locationManager I'm supposed to explicitly check for permissions, which I can't do on runtime since I'm not using API 23 and don't know how to do it on below versions. – MuriloRM Jan 02 '17 at 14:56
  • @VygintasB I understand that now, but that means I need to explicitly ask for permission in another way since I'm using a locationManager method that requires that. What I need to know is how to get this permission on API below 23. – MuriloRM Jan 02 '17 at 14:57
  • There is no concept of *Runtime Permissions* for `API <= 23`. So specifying the location permission in manifest file is enough. – Prerak Sola Jan 02 '17 at 14:58
  • @MuriloRM In this case you just need to check if permission is granted [How can I check permission under API level 23](http://stackoverflow.com/questions/37428464/how-can-i-check-permission-under-api-level-23) – Vygintas B Jan 02 '17 at 15:00
  • @VygintasB that did it! Thank you so much. – MuriloRM Jan 02 '17 at 15:06

1 Answers1

5

As you pointed out, Android API >= 23 added the new runtime permissions.

For older API the user is promped to accept them all on installation

like in the image below

enter image description here

You just need to add them in the manifest and nothing else :)

Pier Giorgio Misley
  • 5,305
  • 4
  • 27
  • 66
  • I don't understand why does Android Studio keeps telling me to explicitly ask for user permission on LocationManager methods though... Am I just supposed to ignore that? – MuriloRM Jan 02 '17 at 14:58
  • Are you going to release this app **ONLY** for android 4.0? Post your build.gradle file please, so we can check it – Pier Giorgio Misley Jan 02 '17 at 15:00
  • Problem was solved by using permissionChecker, thanks for your time anyways. – MuriloRM Jan 02 '17 at 15:06