public class LocalisationService extends Service
{
private LocationManager locationManager;
private BroadcastReceiver receiver;
//Define a listener that responds to location updates
private LocationListener locationListener = new LocationListener()
{
//Called when a new location is found by the network location provider
public void onLocationChanged(Location location)
{
//insertion of the location in the DB
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
@Override
public IBinder onBind(Intent intent){return null;}
@Override
public void onCreate()
{
super.onCreate();
int interval = 1; //Specify the update interval
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, interval, 0, locationListener);
IntentFilter filter = new IntentFilter();
filter.addAction("STOP_LOCALISATION_SERVICE");
receiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
stopSelf();
}
};
registerReceiver(receiver, filter);
}
@Override
public void onDestroy()
{
unregisterReceiver(receiver);
locationManager.removeUpdates(locationListener);
super.onDestroy();
}
}