Location Services and GPS on Android

Its fun to play around with the cool devices in your phone. ADK makes this kind of stuff easy and fun, but there are quite a few steps to get an app to use the devices. Let’s put all the pieces together – not to make a pretty app, but just to mess around and see what can be done. I’ve got two devices a Samsung Tab 7.0+ (ADK 3.2) and a Samsung Nexus S (ADK 4.0.4) and I’ll use mostly a ThinkPad with Eclipse and ADK running on it for deploying apps to the phones. I may also try all of this from a Mac.

Location Services

Google has some pretty good documentation. There’s also sample apps in the SDKs. I’ll be starting with the docs. First thing to do is get an app started. I’ve got a  basic app created for testing purposes that I built in my previous post on android development. So step one is to add the necessary permissions to the AndroidManifest.xml file:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />

You can either cut/paste these or use the wizard….I’ll go wizard–> Open the file and go to the permissions tab. Click Add and then Uses Permission. You’ll get a list box on the right – use that to pick off android.permission.ACCESS_COARSE_LOCATION. Repeat the process to pick off the other two. Now go check the XML code to see where ADK placed the lines – they should be up near the top of the file.

Following along with the sample app assumes quite a bit of knowledge of what is going on with the droid lifecycle. There’s a couple of things we need to do – we need to make sure that a location provider is enabled when our app is starting. We also need to make sure we have proper variables declared to handle starting the GPS and laying the ground work for displaying data on the activity.

However before we get to that – you should make sure you have the proper version of the Android support libraries installed and tied into your project. Open up the ADK manager and check the extras section. Make sure you have it installed Once you have that you need to copy it over into your project. Go to the file system, create a libs directory, copy the jar into there, go back to eclipse, refresh your project, find the jar, right click on it, and pick off Add To Build Path. Intuitive – right? No…d’oh.

An easier approach – right click your project, pick Android Tools, and Add Support Library – that’s much better – come on Google update your docs!

onStart() and onCreate
Create an onStart() function – this function is normally only used when you really need to check things like service availability so this is a good example. Here’s the lines that need to be added:

LocationManager lm = (LocationManager) getSystemService (Context.LOCATION_SERVICE);
final boolean gpsEnabled = lm.isProviderEnabled(lm.GPS_PROVIDER);

Always call the super – then check to see if the GPS is working. What you do from here out is up to you – google’s examples use some nifty fragments to throw up a dialog and prompt the user to turn it on. Cool stuff – if you do this make sure you change your activity from extending Activity to extending FragmentActivity.

In the onCreate() method you’ll need to create a LocationManager instance (add a private instance variable to hold it):

public void onCreate(Bundle savedInstanceState) {
manager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

See if it works – turn your GPS off – see what happens, turn it on…cool! Once you are done messing around don’t forget to make an onStop() method so that you won’t leave the service running and killing the battery on your device. The rest of the example from google is a great one. Well written, it works, and it contains many features that you will need eventually (services, UI interactions, async tasks, screen rotations, etc.).



  1. huan

    Hey Beren,

    Good stuff – I saw you were using OS-X a bit. I’m having some trouble getting the emulators to start. I’m using Juno and ADK (latest rev). The emulator starts and then stops immediately – I don’t get any errors. Ever see that kind of thing happen?

    1. huan

      I did Google around quite a bit with no luck – any suggestions would be grea.t

      1. Beren (Post author)

        Hey Huan – hound of valinor! Nice
        I had a problem that sounds similar. Open up the virtual device manager. Select the one you are launching, and click edit. Is the snapshot feature enabled? If it is turn it off and then try again.

        Somehow I had a stale emulator and this was causing it to not start up. Doing this fixed it, then you can re-enable the snapshot feature.

        Hope that works for you.


        1. huan

          Perfect – that worked! Thanks a lot Beren!

          Like your blog design – nice articles too – quite handy with all the links in the articles.

  2. Pingback: ‘Droid Layouts and UI Design » Tarn Aeluin

Leave a Reply