1

I am developing a app where I need to updated my values every 15 min. For that i am using services.I came across different types of services. Like for long running services we use simple service and, for interacting for other components we use bind service, foreground service. etc....

My situation is like i need to run the service for every 15 min when my app is open and stop the service when my app is closed

I have tried with bind service using http://www.truiton.com/2014/11/bound-service-example-android/ but i am unable to do that,I am unable to run the service every 15 min can any one help me. Thanks in advance.

Sanjeev
  • 292
  • 2
  • 5
  • 24
  • you are starting the service from the activity right ? and the service is running in the background always. when the activity stops then you want to stop the service ? – Sagar Nayak Apr 02 '16 at 06:13
  • Your title and question have confused me. Your title says, you want to know how to stop the service and your question says you want to know how to start service in every 15 mins. Please tell me exactly what you want ? – Varun Kumar Apr 02 '16 at 06:13
  • @sagar yes you right.I want to stop the service,when my gets closed – Sanjeev Apr 02 '16 at 06:19
  • I have given my answer. please check it. – Sagar Nayak Apr 02 '16 at 06:20
  • @varun I want to start the service when my app is opened and stop the service when my is closed. – Sanjeev Apr 02 '16 at 06:21

2 Answers2

1

To start the service when your app starts, start it in onCreate() method:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Intent intent = new Intent(MainActivity.this, MyService.class);
    startService(intent);

}

To stop the service, you can implement the code in 3 different methods namely, onPause(), onStop() and onDestroy().

Calling stopService() in onPause() will stop the service as soon as some other event happens on your device like a phone call, which is not best way to stop since the user will return back to the Activity immediately as soon as the call finishes.

Calling stopService() in onDestroy() is also not the best solution because onDestroy is not called in all the ways a user can close an Android app.

Therefore, the best way to stop the service is by calling stopService() in the onStop() method as shown below.

@Override
protected void onStop() {
    super.onStop();
    Intent intent = new Intent(MainActivity.this, MyService.class);
    stopService(intent);
}
Varun Kumar
  • 1,241
  • 1
  • 9
  • 18
0

If you want to stop service when your activity is closing then you have to implement the code inside onDestroy().

Below is an example-

@Override
protected void onDestroy() {
    super.onDestroy();

    Intent intent = new Intent(MainActivity.this, MyService.class);
    stopService(intent);
}

This will your stop your service.

Unless you don't call finish() in the activity or you explicitly stop the app the onDestroy() don't gets called and your service will run even your calling activity is onPause (in background).

Similarly if you want to start your service activity start you implement in onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent intent = new Intent(MainActivity.this, MyService.class);
    startService(intent);
}

let me know if it help your problem .

Sagar Nayak
  • 2,138
  • 2
  • 19
  • 52
  • suppose if i am in activity A where i have started my service and if i move to different activity say B, will the service get stopped or will it be running? – Sanjeev Apr 02 '16 at 06:23
  • No your service still run if you go to different activity . – Sagar Nayak Apr 02 '16 at 06:25
  • Can we implement Intentservice here – Sanjeev Apr 02 '16 at 06:48
  • i would not suggest using it .(The IntentService cannot run tasks in parallel. Hence all the consecutive intents will go into the message queue for the worker thread and will execute sequentially.) here is a link for your reference - http://stackoverflow.com/questions/15524280/service-vs-intentservice – Sagar Nayak Apr 02 '16 at 06:56
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/108019/discussion-between-sanjeev-and-sagar-nayak). – Sanjeev Apr 02 '16 at 07:51