How to start a service to repeat something in android

Possible duplicate:
Regular Android task (cronjob equivalent)

I am currently trying the following code to complete a task

public class BackupService extends Service {

    private Timer timer = new Timer();
    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
        startBackup();
        return START_STICKY;
    }

    private void startBackup() {

        Date date = new Date(time);
        System.out.println("Backup time:" +date);
        timer.scheduleAtFixedRate(new BackupTimerTask(), date,
                delayTime());
    }

    private long delayTime() {

        long delay = 86400000;
            System.out.println("delay time:" + delay);
        return delay;
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        if (timer != null){
            timer.cancel();
            }
            Toast.makeText(this, "Service Destroyed", Toast.LENGTH_SHORT).show();
    }

  private class BackupTimerTask extends TimerTask {
        @Override
        public void run() {
            System.out.println("Backup started");
            //starting backup here
        }

    }

}

      

and i call this service as

startService(new Intent(this, BackupService.class));

      

This works fine if I overlay a short interval like 5 minutes, but it doesn't work for long intervals. And if I go to start services in Android apps, I can see that my service is up and running. I think there is something wrong with the timertask class. How can I solve my problem?

+3


source to share


2 answers


Milos makes some good points, but please don't put your service in the foreground. Use AlarmManager instead. There are several related questions you can take a look at:

Android AlarmManager - Scheduling a recurring intent to shutdown twice



AlarmManager not working

etc...

+3


source


Actually, in Android there are no "inconvenient tasks" that can be performed with a 100% guarantee. If you look at Android lifecycle management, if the device needs resources, the system will kill some unused or old tasks.

This is the same for Android services. The service has great privilege as a simple task, but the service can still be killed by the system.



The problem in your case is probably that when you set the timer to 5 minutes your service is working correctly. The problem arises when you lengthen the time significantly. Then the likelihood of the service being killed is much greater: see http://www.androidguys.com/2009/09/09/diamonds-are-forever-services-are-not/

So, you will need to consider a different approach to achieving your goal. One solution, but still no 100% guarantee, is to use foreground services: http://developer.android.com/guide/components/services.html#Foreground

+1


source







All Articles