0

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);

    }

  }
Mr Asker
  • 2,300
  • 11
  • 31
  • 56

2 Answers2

3

you have to bind service, like here in doc. there is a lot of questions with answers and even working examples (here probably the best) about that on stack. then you may decide when you want to unbind and destroy service and when to leave it in background. other Activities may also bind to this Service and communicate as well

Community
  • 1
  • 1
snachmsm
  • 17,866
  • 3
  • 32
  • 74
1

you can put your app in the background instead off finishing it.

@Override
public void onBackPressed() {
 moveTaskToBack(true);
// super.onBackPressed();
}
Mina Farid
  • 5,041
  • 4
  • 39
  • 46