How to detect the Location Service on/off for Android

Hi guys,

I want to detect if the Locations Services (GPS) is enabled at program startup. I know this can be done with the event.errorCode for iOS, but what about Android.

Leif

Have you tried? 

You need to make sure to get the relevant permissions in your build.settings file.

Rob

Of course I have tried. I have the following in my build.settings:

android =

    {

        usesPermissions =

        {

            – Permission to retrieve current location from the GPS.

            “android.permission.ACCESS_FINE_LOCATION”,

            – Permission to retrieve current location from WiFi or cellular service.

            “android.permission.ACCESS_COARSE_LOCATION”,

        },

    },

    

–[[

– Android permissions

androidPermissions = {

  “android.permission.INTERNET”,

  “android.permission.ACCESS_FINE_LOCATION”,

  “android.permission.ACCESS_COARSE_LOCATION”,

  },

GPS is working fine, but when it is disabled there are no events trigged, so I cannot read the event.errorCode. In Corona Docs is also stated: “This flag will be non-nil if the user disables Location Services for this app (on iOS)”. From the statement “on iOS” I indirectly read “not on Android”.

Am I wrong?

Leif

Then I would say Android may not provide an event letting you know it’s been turned off.

Rob

Rob,

A search on Google reveals that it can be done with Android SDK, so my hope was that it also could be possible with Corona. But since you are a Corona Staff member, I assume your answer is “Corona official”.

Leif

I wouldn’t say it’s Corona official.  If you’ve tried it and it doesn’t work and our docs say it doesn’t work but you’ve found that it’s possible, that means that we haven’t implemented it yet.  Perhaps when we did, it was at a time of an older Android that didn’t support it and we simply haven’t gotten back around to implementing it. 

I’ll ask engineering and see if this is something easy for them to do and if it’s something that could happen fairly soon.  Since you are a starter account, it would be something that would likely be implemented first in a daily build and if it doesn’t make the next public build (and we’ve kind of decided the feature set for that already), it will be several months before the next public build, so if you are not planning on upgrading to a Pro or Enterprise subscription in the next couple of months, you should plan to work around this, if we can’t get it in the next public build. 

Rob

I’ve heard back from Engineering.  They are investigating adding this support, but it may be a while before it gets in.  In the mean time, you can work around this by using a timer set for say 3 seconds or so, and if you do not get location data in that time, you can assume the service is not available or you’re where it can’t talk to the GPS satellites, like inside a building.

Rob

Have you tried? 

You need to make sure to get the relevant permissions in your build.settings file.

Rob

Of course I have tried. I have the following in my build.settings:

android =

    {

        usesPermissions =

        {

            – Permission to retrieve current location from the GPS.

            “android.permission.ACCESS_FINE_LOCATION”,

            – Permission to retrieve current location from WiFi or cellular service.

            “android.permission.ACCESS_COARSE_LOCATION”,

        },

    },

    

–[[

– Android permissions

androidPermissions = {

  “android.permission.INTERNET”,

  “android.permission.ACCESS_FINE_LOCATION”,

  “android.permission.ACCESS_COARSE_LOCATION”,

  },

GPS is working fine, but when it is disabled there are no events trigged, so I cannot read the event.errorCode. In Corona Docs is also stated: “This flag will be non-nil if the user disables Location Services for this app (on iOS)”. From the statement “on iOS” I indirectly read “not on Android”.

Am I wrong?

Leif

Then I would say Android may not provide an event letting you know it’s been turned off.

Rob

Rob,

A search on Google reveals that it can be done with Android SDK, so my hope was that it also could be possible with Corona. But since you are a Corona Staff member, I assume your answer is “Corona official”.

Leif

I wouldn’t say it’s Corona official.  If you’ve tried it and it doesn’t work and our docs say it doesn’t work but you’ve found that it’s possible, that means that we haven’t implemented it yet.  Perhaps when we did, it was at a time of an older Android that didn’t support it and we simply haven’t gotten back around to implementing it. 

I’ll ask engineering and see if this is something easy for them to do and if it’s something that could happen fairly soon.  Since you are a starter account, it would be something that would likely be implemented first in a daily build and if it doesn’t make the next public build (and we’ve kind of decided the feature set for that already), it will be several months before the next public build, so if you are not planning on upgrading to a Pro or Enterprise subscription in the next couple of months, you should plan to work around this, if we can’t get it in the next public build. 

Rob

I’ve heard back from Engineering.  They are investigating adding this support, but it may be a while before it gets in.  In the mean time, you can work around this by using a timer set for say 3 seconds or so, and if you do not get location data in that time, you can assume the service is not available or you’re where it can’t talk to the GPS satellites, like inside a building.

Rob