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. Calling an API using POST HTTP.

I'm working on an application that should connect to twitxr API.
As seen on the documentation :
The requests are done like this:

I have no truble working with methods that requires a GET HTTP and
authentication, so I'm sure my authetication works fine.

But I can't make postUpdate method (the only one that uses POST HTTP)
to work,
the API keeps returning me the xml that meens something went wrong.

I've test the post vars I'm sending by using a php test page and
everything sounds all right.

Here's my code :

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://" + user_login + ":" +
md5(user_password) + "");

        // add vars to the request
        List<NameValuePair> nameValuePairs = new
        nameValuePairs.add(new BasicNameValuePair("place", place));
        nameValuePairs.add(new BasicNameValuePair("text", text));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);


catch (ClientProtocolException e)
        // TODO Auto-generated catch block


catch (IOException e)
    // TODO Auto-generated catch block


What could be the problem here ?
Is there any trick that I should be aware concerning posting values ?
special encoding for POST vars ?
Is List<NameValuePair> nameValuePairs = new
ArrayList<NameValuePair>(); is the good way to send POST vars ?

Thanks a lot for your help.


2. Playing audio to bluetooth headset


Can we play the audio(amr file) to the Bluetooth headset(no A2DP
support) by using android 2.1/2.2 version APIs ?,  If so can you
suggest me which API i have to use to do the same ?.



3. Does Application update uninstall application

4. Comparing results of query from 2 different databases

5. in spinner items display the list array to replace drawable images how can implemented

6. onBackPressed() not working

7. onClick for the whole screen?