Code: Select all
package com.example.sei;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
public class GpsLocation {
public LocationManager lManager = null;
public boolean GpsEnabled, NetLocEnabled;
double Latitude, Longitude;
float Accuracy;
public MyActivity callback;
GpsLocation(){}
public boolean StartGPS(Context c)
{
lManager = (LocationManager) c.getSystemService(Context.LOCATION_SERVICE);
Accuracy = 9999;
if(lManager == null)
return false;
if(!IsLocationAvailable(c))
return false;
if(!GpsEnabled && !NetLocEnabled)
return false;
if(GpsEnabled)
{
lManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
if(NetLocEnabled)
{
lManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}
callback = (MyActivity) c;
return true;
}
public boolean IsLocationAvailable(Context c)
{
GpsEnabled = false;
NetLocEnabled = false;
if(lManager == null)
lManager = (LocationManager) c.getSystemService(Context.LOCATION_SERVICE);
try {
if(lManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
GpsEnabled = true;
if(lManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
NetLocEnabled = true;
} catch (Exception e) {
e.printStackTrace();
}
if(GpsEnabled || NetLocEnabled)
return true;
return false;
}
LocationListener locationListener = new LocationListener()
{
public void onLocationChanged(Location location)
{
if(location.getAccuracy() <= Accuracy)
{
Accuracy = location.getAccuracy();
Latitude = location.getLatitude();
Longitude = location.getLongitude();
callback.GpsUpdate(Accuracy, Longitude, Latitude);
}
}
@Override
public void onProviderDisabled(String provider) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
};
public void StopGPS()
{
lManager.removeUpdates(locationListener);
}
}
// In your activity, create the GpsLocation class and create this method:
public void GpsUpdate(float Accuracy, double Longitude, double Latitude)
{
}
Anyway, just thought i'd share... i'm not too great at java yet, so there is probably a better way to do it though. ^_^