0

I know that the question was asked few times but I still don't understand. What is the proper way to implement such an operation - for example downloading some data from web e.g. every 15 minutes. I want to run this operation even after rebooting android (without starting an application by user). Should I use just a AlarmManager + Broadcast Receiver or AlarmManager + Service (IntentService?) or what? :)

latata
  • 1,703
  • 5
  • 27
  • 57
  • you should used broad cast BroadcastReceiver . – Hemantvc Oct 03 '13 at 07:02
  • 1
    I my opinion you really should use BroadcastReceiver + AlarmManager. Getting the BootCompleted event, and running your task every 15min. – BigbangRevo Oct 03 '13 at 07:07
  • Java gives TimerTask that allows to schedule a task after specified amount of milliseconds. You can get a good tutorial with working example in below mentioned link http://enos.itcollege.ee/~jpoial/docs/tutorial/essential/threads/timer.html – Gaurav Gupta Oct 03 '13 at 07:09

5 Answers5

2

To perform a task after 15 minutes, you can use AlarmManager for that.
And since you need to continue even after reboot you need additional Boot Receiver.

So your code flow like below

1.Schedule alarm to fire in every 15 minutes on start of your app.Your alarm can start a service to perform what ever task you need to do.

2.OnReceived of BootReceiver again you have to schedule your alarm.

Lazy Ninja
  • 22,342
  • 9
  • 83
  • 103
Rasel
  • 15,499
  • 6
  • 40
  • 50
  • +1 this this the only right way. Timer and Executors not for this user case. There are tonns of examples how to implement this. – Leonidos Oct 03 '13 at 07:09
  • But what when I schedule alarms in two places - on app start and onReceived in BootReceiver? Can I check if alarm has been already set? – latata Oct 03 '13 at 14:56
  • If device gets shut down,all scheduled alarm will be cleared. So you need to reschedule it again.And yes you can check whether an alarm is already set, please check the link.. http://stackoverflow.com/questions/4556670/how-to-check-if-alarmmamager-already-has-an-alarm-set – Rasel Oct 04 '13 at 03:14
1

There's also the option of a ScheduledExecutorService.

Like this:

private ScheduledExecutorService scheduler;

...

        scheduler = Executors.newSingleThreadScheduledExecutor();

        scheduler.scheduleAtFixedRate(new Runnable()
        {
            public void run()
            {
                // do your stuff
            }
        }, 0, 15, TimeUnit.MINUTES);
fasteque
  • 4,309
  • 8
  • 38
  • 50
  • Beware, [`TimeUnit.MINUTES` is not guaranteed to exist on Android](http://stackoverflow.com/questions/3472423/android-java-util-concurrent-timeunit-convert-milliseconds-to-minutes) – Justin Muller Jan 31 '14 at 16:30
0

I think your best option is using the AlarmManager

This class provides access to the system alarm services. These allow you to schedule your application to be run at some point in the future. When an alarm goes off, the Intent that had been registered for it is broadcast by the system, automatically starting the target application if it is not already running. Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted.

For more details take a look at the documentation for the AlarmManager or refer to this blog post to see how it is implemented

Hannoun Yassir
  • 20,583
  • 23
  • 77
  • 112
0
// try this
 Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        // write your code here
                    }
                });
            }
        },0,9000000);

// when no longer required call 
   timer.cancel();
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
-1

Just use TimerTask to repeat it on particular time interval.

//Declare the timer
Timer t = new Timer();
//Set the schedule function and rate
t.scheduleAtFixedRate(new TimerTask() {

    @Override
    public void run() {
        // Call your service or broadcasting or whatever here that is going to be repeated.
        //Called each time when 1000 milliseconds (1 second) (the period parameter)
        // Here you can specify that this action repeat after whatever time.
    }

},
//Set how long before to start calling the TimerTask (in milliseconds)
0,
//Set the amount of time between each execution (in milliseconds)
1000);
Jitesh Dalsaniya
  • 1,917
  • 3
  • 20
  • 36