4

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 its entry from the database and along with that other services which keep running when the app is activated.

So I want to write a function which will be executed after every hour (when the app is activated) and will give a notification to user just to remind him/her about the service which is running .If the user had forgot to stop the service then they can stop it or continue with service.

What is the best efficient way of doing this. I dont want to drain too much of battery with thihs 1 hour basis check if the user considers it to run for a day or so. Please advice. Thanks :)

3 Answers 3

6

I suggest the code will be like this.

// the scheduler
protected FunctionEveryHour scheduler;


// method to schedule your actions
private void scheduleEveryOneHour(){

        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, 
                                                                 new Intent(WAKE_UP_AFTER_ONE_HOUR), 
                                                                 PendingIntent.FLAG_UPDATE_CURRENT);

        // wake up time every 1 hour
        Calendar wakeUpTime = Calendar.getInstance();
        wakeUpTime.add(Calendar.SECOND, 60 * 60);

        AlarmManager aMgr = (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 = new FunctionEveryHour();
registerReceiver(scheduler , new IntentFilter(WAKE_UP_AFTER_ONE_HOUR));

// broadcastreceiver to handle your work
class FunctionEveryHour extends BroadcastReceiver{
        @Override
        public void onReceive(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 like
                boolean mContinue = getUserPreference(USER_CONTINUE_OR_NOT);//

                if(mContinue){
                        scheduleEveryOneHour();
                } 
        }
}

hope that helps :)

2
  • I have one dout wakeUpTime.add(Calendar.HOUR, ONE_HOUR); what should I put in One_HOUR ? 60,000 ms ??
    – ik024
    Aug 19, 2014 at 6:24
  • ive edited my answer to Calendar.SECOND 60*60. you can also do HOUR or HOUR_OF_DAY , HOUR will be 12 hour clock which will be current hour + 1 % 12, or if HOUR_OF_DAY will be 24 hour clock current_hour + 1 % 24. thats it.
    – Spurdow
    Aug 19, 2014 at 6:54
2

Use AlarmManager refer this and tutorial with PendingIntent

2

Try this way,hope this will help you to solve your problem.

new Timer().scheduleAtFixedRate(task, after, interval);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.