Gps Helper : Gps « Hardware « Android






Gps Helper

  
//package djrain.lib;

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.util.Log;

public class GpsHelper implements LocationListener {
  private static final String tag = "LocationHelper";
  private String bestProvider;
  private LocationManager locationManager;

  private OnLocationChangeListener listener;

  private Location location;
  private String gpsProvider;

  static long MINTIME = 3000;
  static float MINDISTANCE = 0f;
  static float SPEED_UNIT = 1f;


  static public interface OnLocationChangeListener
  {
    public void onLocationChanged(Location location);
  }

  void setOnLocationChangeListener(OnLocationChangeListener listener)
  {
    this.listener = listener;
  }

  public Location getLocation()
  {
    return location;
  }

  public GpsHelper(Context context)
  {
    locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    findBestProvider();
  }

  public void findBestProvider()
  {
    // if(bestProvider != null &&
    // locationManager.isProviderEnabled(bestProvider)){
    // }

    Criteria criteria = new Criteria();
    bestProvider = locationManager.getBestProvider(criteria, false);
    location = locationManager.getLastKnownLocation(bestProvider);
    Log.e("Lilo", "find best provider:" + bestProvider);
    printProvider(bestProvider);

    // if(bestProvider == null)
    {
      gpsProvider = LocationManager.GPS_PROVIDER;
      bestProvider = gpsProvider;
    }
  }

  protected void onResume()
  {
    locationManager.requestLocationUpdates(bestProvider, MINTIME, MINDISTANCE, this);
  }

  protected void onPause()
  {
    locationManager.removeUpdates(this);
  }

  
  public void onLocationChanged(Location location)
  {

    if (location == null)
      return;

    printLocation(location);

    this.location = location;

    if (listener != null)
    {
      listener.onLocationChanged(location);
    }
  }

  public void onProviderDisabled(String provider)
  {
    Log.e(tag, "onProviderDisabled:  " + provider);
  }

  public void onProviderEnabled(String provider)
  {
    Log.e(tag, "onProviderEnabled:  " + provider);
  }

  public void onStatusChanged(String provider, int status, Bundle extras)
  {
    Log.e(tag, "onStatusChanged:  " + provider + "," + status);
  }

  private void printLocation(Location location)
  {
    if (location == null)
    {
      Log.e(tag, "Location[unknown]");
    }
    else
    {
      Log.e(tag, "" + location.getSpeed());
      Log.e(tag, "" + location.getLongitude());
      Log.e(tag, "" + location.getLatitude());
    }
  }

  private void printProvider(String provider)
  {
    LocationProvider info = locationManager.getProvider(provider);
    StringBuilder builder = new StringBuilder();
    builder.append("LocationProvider[").append("name=").append(info.getName()).append(",enabled=").append(locationManager.isProviderEnabled(provider)).append(",getAccuracy=")
        .append(info.getAccuracy()).append(",getPowerRequirement=").append(info.getPowerRequirement()).append(",hasMonetaryCost=").append(info.hasMonetaryCost()).append(",requiresCell=")
        .append(info.requiresCell()).append(",requiresNetwork=").append(info.requiresNetwork()).append(",requiresSatellite=").append(info.requiresSatellite()).append(",supportsAltitude=")
        .append(info.supportsAltitude()).append(",supportsBearing=").append(info.supportsBearing()).append(",supportsSpeed=").append(info.supportsSpeed()).append("]");
    Log.e(tag, builder.toString());
  }
}

   
    
  








Related examples in the same category

1.GpsUtils contains code snippets for GPS.