• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Running background Services on a schedule  RSS feed

 
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • 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.
 
Ranch Hand
Posts: 643
Android Eclipse IDE Java
  • 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
 
Author
Posts: 131
  • 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.
 
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • 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
Posts: 142
  • 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
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!