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

前端 未结 2 741
执笔经年
执笔经年 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条回答
  • 1) To handle alarm at exact times you have 2 options: a) Set minimum SDK level at 18 and not 19. This will make your app work on kitkat at the exact time b) Use the setExact() method to tell Kitkat to keep your alarm at the exact time specified Source: http://developer.android.com/about/versions/android-4.4.html

    2) The boot completed notification is the best way to go. You can set up a Broadcast receiver to get the notification on a reboot. You can store alarms in a Database and retrieve on reboot.

    2.1) It is horrible programming practice to do something the user did not want you to do, i.e. set an alarm even though user has not opened the app.

    3) As long as you don't create files on the SD card, all application data is removed upon uninstall of the app

    4) You should lock/unlock data when writing or reading from it to solve your 12AM problem. If the data is locked, it would not get affected until the user has committed the transaction. If you are using SQLite you can get more information on this from: http://www.sqlite.org/lockingv3.html

    0 讨论(0)
  • 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
        }
    }
    
    0 讨论(0)
提交回复
热议问题