Android Execute A Function After 1 Hour
I have an android application where I am storing user's data on database when he/she activates the app. My app requires the user to stop the application manually in order to remove
Solution 1:
I suggest the code will be like this.
// the schedulerprotected FunctionEveryHour scheduler;
// method to schedule your actionsprivatevoidscheduleEveryOneHour(){
PendingIntentpendingIntent= PendingIntent.getBroadcast(this, 0,
newIntent(WAKE_UP_AFTER_ONE_HOUR),
PendingIntent.FLAG_UPDATE_CURRENT);
// wake up time every 1 hourCalendarwakeUpTime= Calendar.getInstance();
wakeUpTime.add(Calendar.SECOND, 60 * 60);
AlarmManageraMgr= (AlarmManager) getSystemService(ALARM_SERVICE);
aMgr.set(AlarmManager.RTC_WAKEUP,
wakeUpTime.getTimeInMillis(),
pendingIntent);
}
//put this in the creation of service or if service is running long operations put this in onStartCommand
scheduler = newFunctionEveryHour();
registerReceiver(scheduler , newIntentFilter(WAKE_UP_AFTER_ONE_HOUR));
// broadcastreceiver to handle your workclassFunctionEveryHourextendsBroadcastReceiver{
@OverridepublicvoidonReceive(Context context, Intent intent) {
// if phone is lock use PowerManager to acquire lock// your code to handle operations every one hour...// after that call again your method to schedule again// if you have boolean if the user doesnt want to continue// create a Preference or store it and retrieve it here likebooleanmContinue= getUserPreference(USER_CONTINUE_OR_NOT);//if(mContinue){
scheduleEveryOneHour();
}
}
}
hope that helps :)
Solution 2:
Solution 3:
Try this way,hope this will help you to solve your problem.
new Timer().scheduleAtFixedRate(task, after, interval);
Post a Comment for "Android Execute A Function After 1 Hour"