This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Running background Services on a schedule

 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a UI where user selects a date and time.I have a class that does something(read/write to DB). I wish to trigger the logic of this class at user-defined time. I am thinking of using "Services"(I still haven't written a service as yet). I thought of using something like AlarmServices but am not sure, if the device will "remember" the alarm after a reboot.

Questions:
What do you think is the best approach for a scenario like this?
Does Android have a mechanism to schedule an Application run?
Is running a service indefinitely in the background a good idea?
Can you share some link to examples or reference material that can help me understand Services - their ins and Outs?

Thanks.
 
chetan dhumane
Ranch Hand
Posts: 641
Android Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Monu ,

I also want to design the same service running on the background.
Are their any available classes that will run as background process.

Thanks
Chetan
 
Mark L. Murphy
Author
Ranch Hand
Posts: 131
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you think is the best approach for a scenario like this?


To execute something at a specific time, AlarmManager is a fine choice.

Does Android have a mechanism to schedule an Application run?


AlarmManager will raise an Intent at a specified time -- so long as you have a service or broadcast receiver registered to handle that Intent (e.g., via an intent-filter in AndroidManifest.xml), your code will get control at that time.

Is running a service indefinitely in the background a good idea?


So long as it is not doing anything 99.999% of the time, and so long as it does not take up lots of memory, indefinitely-running services are fine. In other words, it is not so much an issue of running indefinitely as it is using system resources more than a user would want.

Can you share some link to examples or reference material that can help me understand Services - their ins and Outs?


Ummm...I do not believe I have used a service in any of my posts on AndroidGuys.com. The SDK comes with several examples of services (samples/ApiDemos/ in your SDK installation), though they are typically under-documented. I'm sure some of the Android books cover services.

Are their any available classes that will run as background process.


Every application runs in its own process. So, for example, you could have a service in one process and an activity in another, communicating by means of AIDL-defined IPC.
 
Ed Burnette
Author
Ranch Hand
Posts: 142
 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it true that long running services will use CPU fairly if you sleep on a wait() on a monitor rather than waking up from a Thread.sleep(..) and checking for a condition ?
 
Ed Burnette
Author
Ranch Hand
Posts: 142
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure, the more sleeping/waiting you do and the fewer instructions you run the better. If you can't avoid polling, make it user configurable, perhaps with some kind of adaptive scheme where you wait longer and longer each time.
 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Joshua Bloch said: "Know and use the libraries" (Item 30: Effective Java)]

For my problem, I was thinking of having a Service with a Timer and running this service indefinitely(basically, it will be a scheduler). And the Jobs will be in TimerTasks. Whenever user chooses a time in an Activity, I will schedule appropriate Jobs at chosen time. But that was before, Mark posted his reply..

Perhaps, AlarmManager does something similar?

Thanks.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic