wood burning stoves*
The moose likes Android and the fly likes Running background Services on a schedule Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Android
Bookmark "Running background Services on a schedule" Watch "Running background Services on a schedule" New topic
Author

Running background Services on a schedule

Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

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.


[List of FAQs] | [Android FAQ] | [Samuh Varta]
chetan dhumane
Ranch Hand

Joined: Jan 07, 2009
Posts: 629

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


http://www.androcid.com/
Mark L. Murphy
Author
Ranch Hand

Joined: Feb 11, 2009
Posts: 131
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.


Mark Murphy (a Commons Guy)
Author of The Busy Coder's Guide to Android Development http://commonsware.com/Android/
Ed Burnette
Author
Ranch Hand

Joined: Jun 10, 2003
Posts: 142
Thanks Mark. I also found these references:

http://d.android.com/guide/samples/ApiDemos/src/com/example/android/apis/app/AlarmController.html
http://d.android.com/guide/appendix/faq/commontasks.html#broadcastreceivers

And there's a long thread about it here, with some ideas that did and didn't work:
http://groups.google.com/group/android-developers/browse_thread/thread/98aba3b545ba4b16/2e607861ff895a7e


Ed Burnette, Author of Hello Android
Blog: ZDNet's Dev Connection - Twitter: @eburnette
Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 761

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

Joined: Jun 10, 2003
Posts: 142
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

Joined: Oct 12, 2008
Posts: 1369
    
    1

[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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Running background Services on a schedule