Simple Android Speedometer


UPDATE

There’s been quite a bit of activity on this post. Some of the info is old, and people keep asking for the full Eclipse project.

Here’s something even better… Step-by-step tutorial for the simple speedometer:

Nothing but old info below here…

I’ve seen lots of questions concerning the use of the GPS functionality in Android.  It’s super easy, but some of the explanations are needlessly complicated.

The code below is a simple speedometer that uses the GPS chip to show your current speed.  There are optimations that can be made (specifically in the “locationManager.requestLocationUpdates” call), but this is good enough to get you going.  The Android API documentation should fill in the gaps. As always … take it, run, and share with the rest of us.

Check out the interface and class below, and make sure to see the previous post for the “CLocation” class.

import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;

public interface IBaseGpsListener extends LocationListener, GpsStatus.Listener
{
  public void onLocationChanged(Location location);

  public void onProviderDisabled(String provider);

  public void onProviderEnabled(String provider);

  public void onStatusChanged(String provider, int status, Bundle extras);

  public void onGpsStatusChanged(int event);
}
import java.util.Formatter;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

public class CSpeedometer extends Activity implements IBaseGpsListener
{
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.speedometer_main);

    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                                           0,
                                           0,
                                           this);

    this.updateSpeed(null);

    CheckBox chkUseMetricUnits = (CheckBox)this.findViewById(R.id.chkUseMetricUnits);
    chkUseMetricUnits.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
      {
        CSpeedometer.this.updateSpeed(null);
      }
    });
  }

  public boolean onCreateOptionsMenu(Menu menu)
  {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.speedometer_main, menu);
    return true;
  }

  public boolean onOptionsItemSelected(MenuItem item)
  {
    switch (item.getItemId())
    {
      case R.id.menuItemQuit:
        this.finish();
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  public void finish()
  {
    super.finish();
    System.exit(0);
  }

  public void updateSpeed(CLocation location)
  {
    float nCurrentSpeed = 0;

    if( location!=null )
    {
      location.setUseMetricUnits(this.useMetricUnits());
      nCurrentSpeed = location.getSpeed();
    }

    Formatter fmt = new Formatter(new StringBuilder());
    fmt.format(Locale.US, "%5.1f", nCurrentSpeed);
    String strCurrentSpeed = fmt.toString();
    strCurrentSpeed = strCurrentSpeed.replace(' ', '0');

    String strUnits = "miles/hour";
    if (this.useMetricUnits())
    {
      strUnits = "meters/second";
    }

    TextView txtCurrentSpeed = (TextView) this.findViewById(R.id.txtCurrentSpeed);
    txtCurrentSpeed.setText(strCurrentSpeed + " " + strUnits);
  }

  public boolean useMetricUnits()
  {
    CheckBox chkUseMetricUnits = (CheckBox)this.findViewById(R.id.chkUseMetricUnits);
    return chkUseMetricUnits.isChecked();
  }

  public void onLocationChanged(Location location)
  {
    if (location != null)
    {
      CLocation myLocation = new CLocation(location, this.useMetricUnits());
      this.updateSpeed(myLocation);
    }
  }

  public void onProviderDisabled(String provider)
  {
    // TODO: do something one day?
  }

  public void onProviderEnabled(String provider)
  {
    // TODO: do something one day?
  }

  public void onStatusChanged(String provider, int status, Bundle extras)
  {
    // TODO: do something one day?

  }

  public void onGpsStatusChanged(int event)
  {
    // TODO: do something one day?
  }
}

UPDATE

I’ve uploaded the source, layout xml, and manifest xml here:

speedometer_test.zip

 


7 Responses to “Simple Android Speedometer”

  1. Amol Vitekar says:

    Will you please give the total code files such as
    1. main.xml
    2. java file
    3. Manifest files

    please i need this for my project purpose.
    Help me.
    amvi.coep@gmail.com

  2. Cole says:

    If I can find the project files laying around, I’ll be glad to send them your way. I haven’t really looked at this stuff in quite a while, and have reformatted my computer a few times since :) …

    However, this is super simple, the full java source is listed above and the main.xml would be pretty trivial. The only part you’ll need that’s not listed in this post is a class that I talked about here: http://blog.cryptofreek.org/2011/03/17/android-location-class-with-u-s-customary-units-instead-of-metric/

  3. Amol Vitekar says:

    Is this code is working..???

  4. Cole says:

    I have not played with this code in quite some time, and I have no idea where the Eclipse project had gone off too.

    There’s been a good bit of interest in it so I may try to resurrect it soon. Work and family has me pretty busy these days though.

    Really though, all of the code is here. The rest shouldn’t be that hard.

  5. Cole says:

    Hey folks, I’ve updated this post with a link to source files. Have fun!

  6. me follow this tutorial correctly but do not work correctly. I am new for android please some one help me. that will be your special thanks

  7. Cole says:

    Take a look at the new video above. It should clear things up.

Leave a Reply