I am developing an Android application which consists a few activities.
I want to get updates from the GPS (onLocationChanged
, onProviderDisabled
, onProviderEnabled
, onStatusChanged
) no matter in which activity the user is currently using.
Where should I implement the LocationListener
in order to get such a behaviour?
This is the class I have created:
public class MyLocationListener implements LocationListener{
private LocationManager locationManager;
private String provider;
@Override
public void onLocationChanged(Location locationGPS) {
//do something
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
Log.i("===========================", "==============================");
Log.i("onProviderEnabled", "==============================");
Log.i("===========================", "==============================");
}
@Override
public void onProviderDisabled(String provider) {
Log.i("===========================", "==============================");
Log.i("onProviderDisabled", "==============================");
Log.i("===========================", "==============================");
}
Create a
Service
and monitor location there or you can implement aLocationListener
in a separate class and use it in everyActivity
, turning it ononResume()
and shutting downonPause()
Start the location listener on the activity you wants to start the gps. To start the GPS you can use
and to stop the GPS you can use
Its better you write the GPS start on the
onCreate()
/onStart()
and the GPS remove on theonDestroy()
of a service and use that service. Otherwise once you stop the GPS the chance of starting the GPS again is less than 50% in some devices.