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.

5
  • 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 ? Commented Apr 2, 2016 at 6: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 ? Commented Apr 2, 2016 at 6:13
  • @sagar yes you right.I want to stop the service,when my gets closed Commented Apr 2, 2016 at 6:19
  • I have given my answer. please check it. Commented Apr 2, 2016 at 6:20
  • @varun I want to start the service when my app is opened and stop the service when my is closed. Commented Apr 2, 2016 at 6:21

2 Answers 2

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); } 
Sign up to request clarification or add additional context in comments.

34 Comments

Thanks for your answer how can i start the service in separate thread
You'll just need the context of the Activity to start service from a separate thread. Rest all implementation of Service will be the same.
Thread t = new Thread(new Runnable() { @Override public void run() { getTimestamp(); } }); t.start(); can i use this way
Yes, you can. Start the service from the run() method by help of Activity's context.
Hey I am unable to stop the service
|
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 .

5 Comments

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?
No your service still run if you go to different activity .
Can we implement Intentservice here
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 - stackoverflow.com/questions/15524280/service-vs-intentservice

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.