Determine if "location services" are enabled on Android

Hello everyone
For a project that i’m working on i need to have the same functionality that “event.errorCode” return in iOS devices when “location services” are off.
From what i can see in the docs and doing test on Android devices this event.errorCode doesn’t work on Android like in iOS, in iOS this return an error when location services are disabled. I need some way of knowing in Android if this location services are disabled so i can show the user an alert letting them know that they have this services disabled, and some form of link to the settings of the device so they can enable those services.
Does anyone have been working in something like that before?
Cheers. [import]uid: 21197 topic_id: 34928 reply_id: 334928[/import]

I too ran into this…

The best solution I can offer is that if location services are not available - I have found the system returns 0,0 as the lat/long. So unless your user starts up your app some where in the middle of the Atlantic ocean you should be fine.

“The point at which the equator (0° latitude) and the prime meridian (0° longitude) intersect has no real significance but it is in the Gulf of Guinea in the Atlantic Ocean, about 380 miles (611 kilometers) south of Ghana and 670 miles (1078 km) west of Gabon.” [import]uid: 9070 topic_id: 34928 reply_id: 138798[/import]

You’re developing with Corona Enterprise, right?

You need to call the [lua]LocationManager.isProviderEnabled()[/lua] method in Java to determine if a specific location service is currently enabled. Please see Google’s API documentation here…
http://developer.android.com/reference/android/location/LocationManager.html#isProviderEnabled(java.lang.String)

You have to feed the above method a constant for GPS_PROVIDER or NETWORK_PROVIDER to see if that location service is enabled. You should preferable query the system for both.
http://developer.android.com/reference/android/location/LocationManager.html#GPS_PROVIDER
http://developer.android.com/reference/android/location/LocationManager.html#NETWORK_PROVIDER

You should also do the following to see if the device supports location services at all. For example, the 1st generation Kindle Fire, Nook Color, and Nook Tablet do not support location services…
[lua]packageManager.hasSystemFeature(android.content.pm.PackageManager.FEATURE_LOCATION_GPS);
packageManager.hasSystemFeature(android.content.pm.PackageManager.FEATURE_LOCATION_NETWORK);[/lua]

Please see Google’s documentation for the above method here…
http://developer.android.com/reference/android/content/pm/PackageManager.html#hasSystemFeature(java.lang.String)

Please be aware that Kindle Fire HD/HD+ always returns false when calling the above package manager method, even though those devices do support location services. We have not determined why yet, but we suspect that Amazon has either written their own API or they’re not following Google’s documentation (their OS is a fork after all).
[import]uid: 32256 topic_id: 34928 reply_id: 139093[/import]

I too ran into this…

The best solution I can offer is that if location services are not available - I have found the system returns 0,0 as the lat/long. So unless your user starts up your app some where in the middle of the Atlantic ocean you should be fine.

“The point at which the equator (0° latitude) and the prime meridian (0° longitude) intersect has no real significance but it is in the Gulf of Guinea in the Atlantic Ocean, about 380 miles (611 kilometers) south of Ghana and 670 miles (1078 km) west of Gabon.” [import]uid: 9070 topic_id: 34928 reply_id: 138798[/import]

You’re developing with Corona Enterprise, right?

You need to call the [lua]LocationManager.isProviderEnabled()[/lua] method in Java to determine if a specific location service is currently enabled. Please see Google’s API documentation here…
http://developer.android.com/reference/android/location/LocationManager.html#isProviderEnabled(java.lang.String)

You have to feed the above method a constant for GPS_PROVIDER or NETWORK_PROVIDER to see if that location service is enabled. You should preferable query the system for both.
http://developer.android.com/reference/android/location/LocationManager.html#GPS_PROVIDER
http://developer.android.com/reference/android/location/LocationManager.html#NETWORK_PROVIDER

You should also do the following to see if the device supports location services at all. For example, the 1st generation Kindle Fire, Nook Color, and Nook Tablet do not support location services…
[lua]packageManager.hasSystemFeature(android.content.pm.PackageManager.FEATURE_LOCATION_GPS);
packageManager.hasSystemFeature(android.content.pm.PackageManager.FEATURE_LOCATION_NETWORK);[/lua]

Please see Google’s documentation for the above method here…
http://developer.android.com/reference/android/content/pm/PackageManager.html#hasSystemFeature(java.lang.String)

Please be aware that Kindle Fire HD/HD+ always returns false when calling the above package manager method, even though those devices do support location services. We have not determined why yet, but we suspect that Amazon has either written their own API or they’re not following Google’s documentation (their OS is a fork after all).
[import]uid: 32256 topic_id: 34928 reply_id: 139093[/import]

Ok si I finally got time to resume this work.

I could successfully use LocationManager to find out if the Location Services where enabled or not, but i’m facing a big issue.
The only place where the code works its in CoronaApplication.java (I’m working with the App template), i couldn’t make it work in LuaLoader.java because when I try to fetch the LocationManager object I need a context so i can execute:

LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION\_SERVICE);  

The thing is that in LuaLoader.java i didn’t find a way to retrieve a Context so obviously when i put that line of code in some method and call it from Lua code i get a null pointer exception. So I need to either find a way to be able to retrieve LocationManager from LuaLoader.java or find a way to call a method from CoronaApplication.java class from my Lua side code.
Could anyone put me in the right direction? [import]uid: 21197 topic_id: 34928 reply_id: 145287[/import]

Well I solved the problem of fetching the LocationManager using:

LocationManager locationManager =  
 (LocationManager) fParentActivity.getSystemService(Context.LOCATION\_SERVICE);   

The problem now is how to comunicate with lua side because i need to return from java some value that indicates whether we have Location Services enabled or not. [import]uid: 21197 topic_id: 34928 reply_id: 145308[/import]

The Android “Application” class derives from the “Context” class. So, you can derive your own Application class like how it is shown in our sample projects and make it available in the rest of your code.

Alternatively, you can fetch the application context by calling the following method, but note that it will return null until the CoronaActivity has been launched…
[lua]
CoronaEnvironment.getApplicationContext()
[/lua]

Have a look at Corona Enterprise sample project “SimpleLuaExtensions” on how to extend the Lua API in Java and return/receive various values to/from Lua. There are a lot of examples and comments in that code that explains how to work with Lua. [import]uid: 32256 topic_id: 34928 reply_id: 145454[/import]

Oh, and in case you’ve missed it, you can find our Corona Enterprise Java API documentation here…
http://docs.coronalabs.com/native/android/html/overview-summary.html
[import]uid: 32256 topic_id: 34928 reply_id: 145455[/import]

Thanks for the tips Joshua, the SimpleLuaExtension SampleCode was very helpful indeed. Cheers! [import]uid: 21197 topic_id: 34928 reply_id: 145766[/import]

Ok si I finally got time to resume this work.

I could successfully use LocationManager to find out if the Location Services where enabled or not, but i’m facing a big issue.
The only place where the code works its in CoronaApplication.java (I’m working with the App template), i couldn’t make it work in LuaLoader.java because when I try to fetch the LocationManager object I need a context so i can execute:

LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION\_SERVICE);  

The thing is that in LuaLoader.java i didn’t find a way to retrieve a Context so obviously when i put that line of code in some method and call it from Lua code i get a null pointer exception. So I need to either find a way to be able to retrieve LocationManager from LuaLoader.java or find a way to call a method from CoronaApplication.java class from my Lua side code.
Could anyone put me in the right direction? [import]uid: 21197 topic_id: 34928 reply_id: 145287[/import]

Well I solved the problem of fetching the LocationManager using:

LocationManager locationManager =  
 (LocationManager) fParentActivity.getSystemService(Context.LOCATION\_SERVICE);   

The problem now is how to comunicate with lua side because i need to return from java some value that indicates whether we have Location Services enabled or not. [import]uid: 21197 topic_id: 34928 reply_id: 145308[/import]

The Android “Application” class derives from the “Context” class. So, you can derive your own Application class like how it is shown in our sample projects and make it available in the rest of your code.

Alternatively, you can fetch the application context by calling the following method, but note that it will return null until the CoronaActivity has been launched…
[lua]
CoronaEnvironment.getApplicationContext()
[/lua]

Have a look at Corona Enterprise sample project “SimpleLuaExtensions” on how to extend the Lua API in Java and return/receive various values to/from Lua. There are a lot of examples and comments in that code that explains how to work with Lua. [import]uid: 32256 topic_id: 34928 reply_id: 145454[/import]

Oh, and in case you’ve missed it, you can find our Corona Enterprise Java API documentation here…
http://docs.coronalabs.com/native/android/html/overview-summary.html
[import]uid: 32256 topic_id: 34928 reply_id: 145455[/import]

Thanks for the tips Joshua, the SimpleLuaExtension SampleCode was very helpful indeed. Cheers! [import]uid: 21197 topic_id: 34928 reply_id: 145766[/import]