0

I have following task: to make service which executes and gets data from Internet every 10 minutes. I try to use combination Service + Timer, but in this case I can't control service (I executes TimerTask with delay in onCreate() method of service, and when I interrupt service TimerTask won't stopped). I need 2 buttons only: "start service" and "stop service". What is the best way in this case? Thank you for helping.

user1078760
  • 749
  • 2
  • 11
  • 17
  • You need to use the AlarmManager. See this question http://stackoverflow.com/questions/1082437/android-alarmmanager – Justin Breitfeller Dec 20 '11 at 16:04
  • Thank you. But why I can''t use Timer for it? – user1078760 Dec 20 '11 at 16:31
  • There is no need to waste a users memory by keeping your service alive doing nothing for 10 minutes. Also, if the phone is asleep, your timer will not run. By using the AlarmManager, the phone will automatically be woken up every 10 minutes and your service can be called perform its internet download task. – Justin Breitfeller Dec 20 '11 at 16:36
  • Thank you. Please, make answer and I will mark it. – user1078760 Dec 20 '11 at 17:04

2 Answers2

1

You should use the AlarmManager. See this question Android: How to use AlarmManager for help on that. There is no need to waste a users memory by keeping your service alive doing nothing for 10 minutes. Also, if the phone is asleep, your timer will not run. By using the AlarmManager, the phone will automatically be woken up every 10 minutes and your service can be called to perform its internet download task.

Community
  • 1
  • 1
Justin Breitfeller
  • 13,737
  • 4
  • 39
  • 47
0

If I understand you well, you want to know how to stop a TimerTask when you interrupt your Service?! If so, read on, otherwise please explain better.

To stop a TimerTask from executing again (assuming it was scheduled to run repeatedly) you must call the cancel() method. The preferred method is to let the TimerTask itself call the cancel() method after checking some boolean flag which can be set externally.

For example:

class MyTimerTask extends TimerTask() {

    private boolean alive = true;

    public void run() {
        if (alive) {
            // do something
        } else {
            cancel();
        }
    }

    public void stop() {
        alive = false;
    }
}

Then, it's pretty obvious that when you interrupt your service, you just have to call MyTimerTask.stop().

Renato
  • 12,940
  • 3
  • 54
  • 85
  • Thank for reply. I try to explain better: I execute Service, and execute timer (which is member of class) timer.scheduleAtFixedRate(..) in onCreate() method of service. Then service execute onDestroy() method of service, but timer still works. It is good, I need to check Internet service every 5 minutes. But how can I stop timer if I have stopped service already? Or how can I organize checking of web-service every 5 minutes? – user1078760 Dec 20 '11 at 16:10
  • From what you're saying, the Timertask should survive after you kill the Service.... so it must not be a member of Service. Keep the TimerTask reference in some class you can have running all the time. If you don't have a class running all the time, then create one... You should probably start a new thread for that... that said, if you posted some demo code it would be much easier to correct what you're doing wrong. – Renato Dec 20 '11 at 16:26