0

In my app I have a Service that runs when i open my app (through onCreate method) and it just displays a simple notification, so I want the Service to close and re-open ( stopService > startService ) every 60seconds that is set by the timer. How do I set the timer to do it?

baTimá
  • 554
  • 1
  • 10
  • 28

2 Answers2

3

Please, look at and use AlarmManager

With AlarmManager you will be able to allow a service to start at a stablished time, alse you can use the setrepeating alarm to schedule an alarm to go off after a certain amount of time and then repeating constantly as long as you want it to.

j0k
  • 22,600
  • 28
  • 79
  • 90
Volodymyr Lykhonis
  • 2,936
  • 2
  • 17
  • 13
  • I've read it and about TimerTask (which i think it suits best(because its used in a similar app)) and better if you could explain to me for better understanding. – baTimá Oct 09 '12 at 17:51
  • Alright, im using a AlarmManager to run the service. But my question now is: **Can I make a AlarManager to run a Method inside the Service?How?** – baTimá Oct 10 '12 at 19:19
  • to the new question: Im using a Timer to do the job. thanks for the first question: Im using a AlarmManager to start the Service. Thanks. – baTimá Oct 10 '12 at 19:58
  • 1
    U do not need Timer. Pls read info carefully http://developer.android.com/reference/java/util/Timer.html use http://developer.android.com/reference/android/os/Handler.html instead – Volodymyr Lykhonis Oct 10 '12 at 20:08
  • Yep, thanks I had a look at some other application and got some help from my Job, and thanks for helping that is the right thing I needed. But im using a Handler inside a Timer just to `Toast` a message to test, and yes I do need a Timer to every X seconds check my database. – baTimá Oct 10 '12 at 20:18
1

Use AlarmManager to be poked every minute. Then in your BroadcastReceiver's onReceive() poke service to do the job by i.e. doing startService() with special intent.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • Is it possible to make the service run "forever" so the Timer would keep refreshing every X seconds? and as I said i already have a app that uses TimerTask but I think that it does not run all the time. – baTimá Oct 09 '12 at 18:04
  • what you mean by "forever"? There's one thing only you can do forever. It is "busy loop" :) Use `IntentService` for your background job and fire it by recurring alarm. – Marcin Orlowski Oct 09 '12 at 18:56
  • Forever like forever. My app is meant to receive and send messages through a database(thats why it needs to check the database every 60seconds(new messages)). But the point is with `AlarmManager` will it keep running forever? and how do i use the `AlarmManager`? I have been looking through some other questions and blogs but I only get confused and some of them are non-related. – baTimá Oct 09 '12 at 19:06
  • @MrG.Knowledge I meant - do you need doing anything all the time (like say "spinning the wheel"), continuously. Or you want to do something periodically, while the action itself is a one time "job". The only thing this job needs to be done every X seconds/minutes. These two are maybe close, but you take completely different approach for each – Marcin Orlowski Oct 09 '12 at 21:45
  • I need to keep cheking the database every X seconds to see if somethings new(message) so thats why i need a AlarmManager. By the way i solved THIS question, but my other question is [HERE CHECK IT OUT](http://stackoverflow.com/questions/12807577/is-it-possible-to-have-a-alarmmanager-inside-a-service-class) – baTimá Oct 10 '12 at 17:06
  • 1
    @MrG.Knowledge you are going to kill the battery. use push notification or app that populates the DB could broadcast something to let you know? Pulling is rarely a way to go – Marcin Orlowski Oct 10 '12 at 18:33
  • 1
    @MrG.Knowledge then accept answer that helped most or put your own for a record and reference to others in future and then accept your answer to mark question as answered. – Marcin Orlowski Oct 10 '12 at 18:34
  • how do I set the broadcast... Because in the app it got to check the database for new messages EVERY SECOND I should say because its a Message System so it need to be updated a lot. – baTimá Oct 10 '12 at 18:41