Service with timer

by bradleygh...@gmail.com » 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
inconsistent.

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
               }
        }

        @Override
    public void onStart(Intent intent, int startId) {

        super.onStart(intent, startId);

                startService();
    }
}

--~--~---------~--~----~------------~-------~--~----~



Service with timer

by bradleygh...@gmail.com » 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
Loop
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
Loop
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.

Brad

On Apr 27, 8:48pm, "bradleygh...@gmail.com" <bradleygh...@gmail.com>


--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


Other Threads

1. latitude and longitude to Microdegrees

Hello,

I want to dispaly one point on map. for that i will have to create the
object of GeoPoint and we need to pass the altitude and longitude in
it in Microdegrees. so can some one tell me how can i do it ?
--~--~---------~--~----~------------~-------~--~----~

2. How to check if some email client is configured ?

hi,

How to check if some email client is configured ? and if configured how to
get the default email client. THanks


--
Best Regards,
Atif Gulzar

I  Unicode, zln 

--~--~---------~--~----~------------~-------~--~----~

3. running linux commands from java

4. JACxHero 1.4 dgn Kernel Baru..

5. Do they need to change the security keys for the OEM

6. Making procedure calls in Android

7. error msg after run a thread