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. Eclipse + cupcake + other android projects in build path = VerifyError

Hi Guys,

We've had the same problem with ProSyst mBS running on cupcake. After
some checks we found that it depends what JDK you use for building your
jar files before DEXing them. In general here is the info:

We've checked with the following JDK versions: jdk1.5.0_06, jdk1.5.0_10,
jdk1.5.0_12, jdk1.5.0_15, jdk1.6.0_06, jdk1.6.0_10. Only with
jdk1.5.0_06, jdk1.5.0_10 and jdk1.5.0_12 there was no verify errors.

I hope that this can help you.







-- 

Best Regards,
    Daniel
---------------------------------------------------------------
Daniel Janev  Department Manager/Core Platform and Smart Home
ProSyst Software GmbH
1606 Sofia, Bulgaria  Vladajska Str. 48
Tel. +359 (0)2 952 35 81/109  Fax +359 (0)2 953 26 17
Mobile Phone +359 (0)888 678 670
http://www.prosyst.com  d.ja...@prosyst.com
---------------------------------------------------------------
stay in touch with your product.
---------------------------------------------------------------

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

2. Schema validation

Hi folks ,
        I am using JDOM for schema validation but i am getting
exception java.rmi.RemoteException ...
Is the use of JDOM ok or is there any other option available ...can i
get any links for examples...
Any help would be appreciated ...
Thnx in advance ....

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

3. Keeping an eye on the competition...

4. About IMEISV request on libreference-ril

5. Eclipse + cupcake + other android projects in build path = VerifyError

6. problems removing childs from view

7. removing Phone.apk from the device