Schedule an event to be executed in future (Cron Job the Android way)

前端 未结 2 742
执笔经年
执笔经年 2021-02-10 00:00

I have certain dates which once attained lose their relevance and new dates for these fields in the DB should be calculated, I know I can leverage the AlarmManager class for thi

2条回答
  •  长情又很酷
    2021-02-10 00:30

    I think, you can write a Service something similar to the below one (This will do your task for every 5 minutes) and start the Service from the BroadcastReceiver which listens to BOOT_COMPLETED state.

    public class YourService extends IntentService {
    
        public YourService() {
            super("YourService");
        }
    
        public YourService(String name) {
            super(name);
        }
    
        private static Timer timer = new Timer();
    
        private class mainTask extends TimerTask {
            public void run() {
                // TASK WHATEVER YOU WANT TO DO 
            }
        }
    
        public void onDestroy() {
            super.onDestroy();
        }
    
        @Override
        protected void onHandleIntent(Intent intent) {
            timer.scheduleAtFixedRate(new mainTask(), 0, 300000);  // 5 minutes
        }
    }
    

提交回复
热议问题