get current user speed using GPS android
get current speed using GPS android is very useful task used to get current speed of the user with the help of the GPS.
Simple step to get current speed:
1)Check wether the GPS is connected or not using the location Manager.
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
2)If GPS not enable,Show alert to show on the GPS.
gpsManager.showSettingsAlert();
3)Get current speed using GPSManager class,
speed = location.getSpeed();
GPSManager.java
public class GPSManager implements android.location.GpsStatus.Listener
{
private static final int gpsMinTime = 500;
private static final int gpsMinDistance = 0;
private static LocationManager locationManager = null;
private static LocationListener locationListener = null;
private static GPSCallback gpsCallback = null;
Context mcontext;
public GPSManager(Context context)
{
mcontext=context;
GPSManager.locationListener = new LocationListener()
{
@Override
public void onLocationChanged(final Location location)
{
if (GPSManager.gpsCallback != null)
{
GPSManager.gpsCallback.onGPSUpdate(location);
}
}
@Override
public void onProviderDisabled(final String provider)
{
}
@Override
public void onProviderEnabled(final String provider)
{
}
@Override
public void onStatusChanged(final String provider, final int status, final Bundle extras)
{
}
};
}
public void showSettingsAlert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mcontext);
// Setting Dialog Title
alertDialog.setTitle(“GPS is settings”);
// Setting Dialog Message
alertDialog.setMessage(“GPS is not enabled. Do you want to go to settings menu?”);
// On pressing Settings button
alertDialog.setPositiveButton(“Settings”, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mcontext.startActivity(intent);
}
});
// on pressing cancel button
alertDialog.setNegativeButton(“Cancel”, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
public GPSCallback getGPSCallback()
{
return GPSManager.gpsCallback;
}
public void setGPSCallback(final GPSCallback gpsCallback)
{
GPSManager.gpsCallback = gpsCallback;
}
public void startListening(final Context context)
{
if (GPSManager.locationManager == null)
{
GPSManager.locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
}
final Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setSpeedRequired(true);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
final String bestProvider = GPSManager.locationManager.getBestProvider(criteria, true);
if (bestProvider != null && bestProvider.length() > 0) {
GPSManager.locationManager.requestLocationUpdates(bestProvider, GPSManager.gpsMinTime,GPSManager.gpsMinDistance, GPSManager.locationListener);
}
else
{
final List<String> providers = GPSManager.locationManager.getProviders(true);
for (final String provider : providers) {
GPSManager.locationManager.requestLocationUpdates(provider, GPSManager.gpsMinTime,
GPSManager.gpsMinDistance, GPSManager.locationListener);
}
}
}
public void stopListening()
{
try
{
if (GPSManager.locationManager != null && GPSManager.locationListener != null) {
GPSManager.locationManager.removeUpdates(GPSManager.locationListener);
}
GPSManager.locationManager = null;
}
catch (final Exception ex)
{
}
}
public void onGpsStatusChanged(int event) {
int Satellites = 0;
int SatellitesInFix = 0;
int timetofix = locationManager.getGpsStatus(null).getTimeToFirstFix();
Log.i(“GPs”, “Time to first fix = “+String.valueOf(timetofix));
for (GpsSatellite sat : locationManager.getGpsStatus(null).getSatellites()) {
if(sat.usedInFix()) {
SatellitesInFix++;
}
Satellites++;
}
Log.i(“GPS”, String.valueOf(Satellites) + ” Used In Last Fix (“+SatellitesInFix+”)”);
}
}
4)Get current speed in KM/H,
currentSpeed = round(speed,3,BigDecimal.ROUND_HALF_UP);
kmphSpeed =round((currentSpeed*3.6),3,BigDecimal.ROUND_HALF_UP);
public static double round(double unrounded, int precision, int roundingMode)
{
BigDecimal bd = new BigDecimal(unrounded);
BigDecimal rounded = bd.setScale(precision, roundingMode);
return rounded.doubleValue();
}
Screenshot:
1 comment:
The best Code so far.
Post a Comment