0

I have done an application that fire an alarm in certain time, and i am stuck on implementing remind me after half an hour functionality

what can i do to implement receiver, or service or anything that runs after half an hour of clicking the button of reming me after half an hour

any suggestions ?

MBH
  • 16,271
  • 19
  • 99
  • 149
  • Why don't you update the time on 'button of reming me after half an hour' ? – demo_Ashif Dec 11 '15 at 18:44
  • @demo_Ashif the user may leave the app, i need to fire the function even when the user is out of the app. – MBH Dec 11 '15 at 18:45
  • have you looked for timers? android has its own timer class that you can use it as countdown timer – salihgueler Dec 11 '15 at 18:47
  • timer works while user inside the app, when user leaves the app timer does not work! @nomad – MBH Dec 11 '15 at 18:48
  • @MBH then create notification and set the button ! Notification will appear on alarm time with remind button.If the user clears the notifications, nothing happens and notification is cleared. If the user clicks on the notification, then clear the notification and set up alarm after 30 min – demo_Ashif Dec 11 '15 at 18:49
  • You can write a simple service with a timer and whenever the time is up.it can do your thing.all you need to do is start a service with a timer inside of it – salihgueler Dec 11 '15 at 18:50
  • http://stackoverflow.com/questions/3819676/android-timer-within-a-service i am on mobile so maybe you can check this link – salihgueler Dec 11 '15 at 18:54

2 Answers2

2

Edited the code from Android execute a function after 1 hour to half an hour.

// the scheduler
protected FunctionEveryHalfHour scheduler;


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

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

    // wake up time every 1 hour
    Calendar wakeUpTime = Calendar.getInstance();
    wakeUpTime.add(Calendar.SECOND, 30 * 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 FunctionEveryHalfHour();
registerReceiver(scheduler , new IntentFilter(WAKE_UP_AFTER_HALF_HOUR));

// broadcastreceiver to handle your work
class FunctionEveryHalfHour 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 half 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){
                    scheduleEveryHalfHour();
            } 
    }
}
Community
  • 1
  • 1
g90
  • 506
  • 3
  • 18
  • what is this variable WAKE_UP_AFTER_HALF_HOUR ? – MBH Dec 11 '15 at 19:01
  • After half hour the alarmmanager will fire your pending intent of the type WAKE_UP_AFTER_HALF_HOUR for which we are listening this in turn will wake your app and execute the onReceive. Could be named anything but your receiver has to be listening to the same thing. – g90 Dec 11 '15 at 20:33
  • so it is any String ? – MBH Dec 11 '15 at 20:36
  • basically yes it could be any string – g90 Dec 11 '15 at 20:37
0

You can write a simple service with a timer and whenever the time is up.it can do your thing.all you need to do is start a service with a timer inside of it

salihgueler
  • 3,284
  • 2
  • 22
  • 33