Service with timer

by » Mon, 27 Apr 2009 22:07:32 GMT

Sponsored Links
 I have a service running that has a Timer. The Timer schedules a
TimerTask to run at a certain interval. The TimerTask simply display's
a notification. In theory it all sort of works but I find it very

The notifications only occur when the device is not sleeping. Also
sometimes it seems as though the Timer is only counting time when the
device is not asleep. If I leave the device asleep for 15 minutes then
wake it up after a minute or two I will get 4 notifications in a row.
All the ones that should have been displayed periodically over the
last 15 minutes.

Am I handling this situation the correct way? Is a timer the correct
way to do this? How can I make the device wake from sleep and display
the notifications at the correct intervals?

public class GameScoreService extends Service {

    Timer timer = null;

        private void startService() {

                timer = new Timer();

                timer.scheduleAtFixedRate(new getNewScores(), 5000, 180000);

        class getNewScores extends TimerTask {
                public void run() {

                        ...perform notification

    public void onStart(Intent intent, int startId) {

        super.onStart(intent, startId);



Service with timer

by » Mon, 27 Apr 2009 22:44:02 GMT

 Well after a bit more research I think I have concluded that my
initial approach was way off. I am now changing it to use an
AlarmManager instead. Can someone please explain AlarmManagers to me a
bit better.

Previously this is how my app worked.

Add an item to a database.
Launch service.
Service starts a Timer that runs a TimerTask every 5 minutes.
TimerTask = For every row in the database
  If certain conditions are met, fire off a notification.
  If other conditions are met, delete that row
If there are now rows left, stop the service and timer.

So how is the best way to now implement it with an AlarmManager. I
have just set it up as follows.
Add an item to a database.
Start AlarmManager that runs every 5 minutes
Broadcastreceiver = For every row in the database
  If certain conditions are met, fire off a notification.
  If other conditions are met, delete that row
If there are now rows left, cancel the AlarmManager

This seems to work ok. Now I no longer need to run the service I
previously was using at all. What I want to know is how long will the
alarm keep running? Until it is explicitly canceled? Until the device
is rebooted? What if my app is closed?

Basically I just want to know if I am tackling this problem in the
correct manner.


On Apr 27, 8:48pm, "" <>


Sponsored Links

Other Threads

1. Call out to the community about the poor quality of Android's error messaging

you will probably not like what I'm going to say, but: ranting on the forum
is going to have very little results here.

even if there are Android developers who read them (and a fewer that even
answer certain questions) they are all very busy at the moment. what this
means is that the best way to report that kind of problem (i.e. "weak error
reporting") is to *use the public issue tracker*.

   - register an issue, and chances that the problem will be seen and
   reviewed by a relevant member of the development team is increased

   - describe your problem as precisely as possible, trying to nail it to
   the simplest possible code snippet or instructions. this also dramatically
   increases the chances of seeing the issue fixed / answered dramatically in
   the next SDK release

of course, I'm talking about problems that you want to communicate to
Android engineers. feel free to use the forum to ask other members to see if
there aren't simple work-arounds / way of using the system or if they have a
solution for it first.

sadly, your original post didn't contain any meaningful information we can
use to track exactly the error reporting weaknesses you're talking about
(and I certainly don't deny they exist).


2. How to use ANSWER_ACTION

          Can somebody help me in using ANSWER_ACTION . How can i use
it when phone is ringing ??
         I tried-

                              Intent i = new Intent();

              But error no activity found.

Thanks in Advance
Abhinav Parashar

3. GTalk Service IM Documentation + Sample Apps

4. GTalk Data Message

5. Android Programming Guide

6. Mail API

7. Semi-transparency / alpha blending looks awful on Android?