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