0

In my program, I'd like to check for a new version (getting data from HTTP).
The function works perfectly, but I want to have it running in background every X (configurable, one hour, one day, and so on).

So I wrote this code:

    Timer mTimer1 = new Timer();
    PowerManager pm = (PowerManager) this.main.getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = this.pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, MainActivity.PROGRAM);
    PowerManager.WakeLock wl.acquire();
    TimerTask mTt1 = new TimerTask()
    {
      public void run()
      {
        mTimerHandler.post(new Runnable()
          {
            public void run()
            {
// Do the check...
            }
          });
      }
    };

(since this function is in a separate class NOT deriving from Activity, I passed to the constructor the parent class this.main, so that I can call getSystemService. This Variable is declared as private MainActivity main).
Well, when I start the Timer it checks for new version, then, after an hour (so my settings), I check in the Logs and I see that the thread did not run...

I searched in Internet and I found, that I have to use the PARTIAL_WAKE_LOCK, and I do that...

Any idea, why I have this problem?

Thanks a lot! Luca Bertoncello

jimpanzer
  • 3,470
  • 4
  • 44
  • 84

4 Answers4

0

Here is a SO answer that can help you with this using a handler and postDelayed().

However, if you want to run this even when the app isn't open then you will want to use an AlarmManger.You create a PendingIntent and create a Calendar instance to set the time you want it to check then use setRepeating() on the AlarmManger. This will have it check even when not in the app. You can also register it in the manifest as Boot_Completed so it will start running again after reboot when the device is turned off

Here is a good example to get you started with AlarmManager

Community
  • 1
  • 1
codeMagic
  • 44,549
  • 13
  • 77
  • 93
0

If you are using a Timer, did you remember to schedule your TimerTask? Try mTimer1.schedule(mTt1, delay, interval);

And if you want to stop that, do mTimer1.cancel(); and mTimer1.purge(); to clear the schedule queue.

StoneBird
  • 1,900
  • 13
  • 12
0

Use this code

 Timer timer = new Timer();
         timer.scheduleAtFixedRate( 
                    new java.util.TimerTask() {
                        @Override
                        public void run() {
                              // Your code
                   }},
                1000, 5000
                );

Where 1000 is a post delay , if you want delay for first time , if you don't want than put it 0

5000 is a iteration time

Ronak Mehta
  • 5,971
  • 5
  • 42
  • 69
0

When the phone goes to sleep, execution will be paused for your activity and thus your timer will never fire. Using PARTIAL_WAKE_LOCK is probably not a good idea as it will consume battery the entire time your phone sleeps.

I would suggest taking a look at AlarmManager and specifically setInexactRepeating

FlyingStreudel
  • 4,434
  • 4
  • 33
  • 55