ben
ben

Reputation: 393

automatically start up a service when the application is closed

i have created an application which will start a service to check a particular remote file for update..

the service part is done but now the user needs to manually start up the service

is there a way which i can make the service start up automatically once the application is not in view (either the user clicks the home button/clicks back or open another application)

++++++++++ANSWER++++++++++

anyway figured out how to do it already

    @Override
protected void onPause() 
{
    startService(new Intent(this, fileService.class));
    super.onPause();
}

@Override
protected void onResume() 
{
    stopService(new Intent(this, fileService.class));
    super.onResume();
}

i used this to start the service when the application is paused and stop when its resume

Upvotes: 1

Views: 1877

Answers (2)

Metin Ilhan
Metin Ilhan

Reputation: 241

I thought the answer should be clear and simple for futere needings.

@Override
protected void onPause() {
  startService(new Intent(this, YourService.class));
  super.onPause();
}

@Override
protected void onResume() {
  stopService(new Intent(this, YourService.class));
  super.onResume();
}

Upvotes: 1

Peter Knego
Peter Knego

Reputation: 80340

You can not do this on Application level, but on Activities level. Activities have a lifecycle, where they get notified when their status changes.

You need to implement onPause() or onStop() in your Activity.

Upvotes: 3

Related Questions