Simple GPS Android Application

Lately I’m trying to learn how GPS thing is done with Android. As usual anyone can write code to use GPS but optimizing them for our needs and using them efficiently and effectively (remember GPS sucks out the battery) is the game.

Here I’ll share the source code with inline comments.

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;

public class UseGpsActivity extends Activity {
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {

        //reference to the system	location manager
        LocationManager locatonManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

        //define location listener
        LocationListener locationListener = new LocationListener() {
			public void onStatusChanged(String provider, int status, Bundle extras) {
				Toast.makeText(getApplicationContext(), "provider status changed", Toast.LENGTH_LONG).show();
			public void onProviderEnabled(String provider) {
				Toast.makeText(getApplicationContext(), "provider enabled", Toast.LENGTH_LONG).show();
			public void onProviderDisabled(String provider) {
				Toast.makeText(getApplicationContext(), "provider disabled", Toast.LENGTH_LONG).show();
			public void onLocationChanged(Location location) {
				Toast.makeText(getApplicationContext(), "New Location", Toast.LENGTH_LONG).show();

		//Register location manager with the listener
		locatonManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
		locatonManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

At line 51 and 52 you might have noticed that I’m passing location listener reference twice to requestLocationUpdates method. the difference here is I’m using both Network, GPS to locate the current location of the device. If the GPS doesn’t work properly then Network comes into play :) You can always stick to only GPS or Network as you wish depend on your requirement. Again remember GPS access will suck your battery.

Also please remember you’ll need use permission to “ACCESS_FINE_LOCATION”

This is only a heads on, you can read more at Google Android Developer site.

Did you enjoy this article?
Signup today and receive free updates straight in your inbox. We will never share or sell your email address.


  1. Re: Kantha Sasti Kavasam,
    It very good Apps, but once there is an problem when i use in HTC Desire S.
    When we are in third screen, after we press “start Music” it start playing the song, If i go back to second screen by using back key, then i press again “continue” button it move to third screen but it again offer for “Start Music” instead of “Pause” or “Stop”. If press “start music” mistakenly it start play parallel from beginning, result we hearing two voice.

    Solution Required:
    1. In Second screen you should offer three button one is for “start Music” another for ” Stop Music” and another for ” Continue” or “Next page”.
    2. Note once the ” start Music” button activated that button should change as “pause” button, until the complete the song it should offer “start Music” at point of time.
    3. Offer “stop” button in all the screen, otherwise we can only stop this by task manager.
    looking forward your positive response. Please Please do it

    • Thanks for the feedback. I’m working on it starting from today :) Thanks again for using my app.

  2. Yeah that’s what I’m taiklng about baby–nice work!

  3. oh. to trying ….


Leave a Reply