Search code examples
androidandroid-alarms

How to add adding "remind me after half an hour" functionality - android


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 ?


Solution

  • 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();
                } 
        }
    }