I have a Map activity
and MainActivity
. The MainActivity communicate with Service class that contains LocationManager. When the button was clicked in the MainActivity then the Map Activity is opened.
I want to stop the Locationmanager in the service class just when the App was closed or the Trackingservice checkbox button in the menu in the MainActivity was set to false (This is another button than the one which starts the Map activity) but I dont want to stop it when I press the back
button and I want to go from the Map to the MainActivity since with the current state the onBackPressed()
then the onDestroy()
are being invoked in the Map Activity, after that the onDestroy()
method in the Trackingservice
class is being called which leads on to stop the Tracking service.
How can I reach that?
As I said I want to stop the service when the app is being closed in the Background (not matter in the MainActivity or Map activity) or the Tracking service check box button was set to false which works fine at the moment but I dont to stop the service when the back
button was pressed which does not work at the moment because is being stoped.
MainActivity
public class MainActivity extends ActionBarActivity implements
AsyncTaskCallback {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.route_available);
Intent i = new Intent(this, TrackingService.class);
startService(i);
}
@Override
protected void onDestroy() {
super.onDestroy();
Intent i = new Intent(this, TrackingService.class);
stopService(i);
}
}
Service class:
public class TrackingService extends Service implements AsyncTaskCallback,
LocationListener {
LocationManager lm;
private void detectLocation() {
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 15 * 1000, 0,
this);
enableGPS(lm);
}
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(wifi_receiver);
lm.removeUpdates(this);
}
}
Map Activity
public class Map extends FragmentActivity implements OnMapReadyCallback {
@Override
public void onBackPressed() {
super.onBackPressed();
markerMap.clear();
stopAlarm();
}
@Override
protected void onStop() {
super.onStop();
GetLLRD.shouldContinue = false;
}
@Override
protected void onDestroy() {
super.onDestroy();
markerMap.clear();
stopAlarm();
Intent i = new Intent(this, TrackingService.class);
stopService(i);
}
}