icon_android Android

You can now schedule tasks to be performed in a periodic fashion:

  • interval: triggered every X minutes
  • hourly: triggered at designated hours
  • daily: triggered at designated weekdays
  • monthly: triggered at designated months of the year
  • contextual: triggered by a broadcast
  • timer: triggered at the designated date and time

To this effect, we created Schedules. After creating a Schedule, you can manipulate its properties to create flexible and event-based triggers for a multitude of things. The scheduled tasks are persistent and will be automatically rescheduled if the phone is turned off or restarted. Think of it as a powerful IFTTT (If This Then That), where you can “glue” a chain of tasks as long as you wish by combining schedules together. The Schedule is essential a trigger-action agent, with the following public calls:

Creating a Schedule

Scheduler.Schedule schedule = new Scheduler.Schedule("schedule ID"); //you refer to this ID to remove it later

Defining the trigger

  • setInterval( integer ): a number between 5-59. This internal is not exact. Every 5 minutes, a check if performed to evaluate if this much time has elapsed since the last time the scheduler was triggered.
  • addHour( integer ): a number between 0-23. You add more hours by calling the same function in sequence.
  • addWeekday( String ): the weekday in which to trigger this schedule, e.g., Monday, …, Sunday. You add more weekdays by calling the same function in sequence.
  • addMonth( String ): the month in which this trigger is valid, e.g., January, …, December. You add more months by calling the same function in sequence.
  • setTimer( Calendar datetime ): a specific date and time to trigger this task.
  • setContext( String trigger_broadcast ): trigger this task if this broadcast is issued.
  • randomize( Scheduler.RANDOM_TYPE_XXX ): triggers this task on a random fashion. Possible values are Scheduler.RANDOM_TYPE_HOUR, Scheduler.RANDOM_TYPE_WEEKDAY, Scheduler.RANDOM_TYPE_MONTH. If you have defined more than one possible option (hour, weekday, month), this task will be triggered at a random within the defined options.

Defining the action

  • setActionType( Scheduler.ACTION_TYPE_XXX ): specifies what happens when the trigger is executed. Possible values are Scheduler.ACTION_TYPE_BROADCAST, Scheduler.ACTION_TYPE_ACTIVITY, and Scheduler.ACTION_TYPE_SERVICE.
  • setActionClass( String XXX ): if the action type is a broadcast, you specify here the broadcasted String. However, if the action type is either an activity or service you wish to start, you specify it with package/package.activity or package/package.service, respectively. For example, if you wish to start the alarm clock in the phone, you would put: “com.android.deskclock/com.android.DeskClock”
  • addActionExtra( String key, Object value ): you can add as many extras you need when the broadcast, activity or service is started. The value is any of the following: String, integer, double, long, boolean.

Saving the schedule

Scheduler.saveSchedule(getApplicationContext(), schedule);

Removing the schedule

Scheduler.removeSchedule(getApplicationContext(), "schedule_id");

Scheduling an ESM every day at 9AM

try{
            String esm_goodmorning = "[{'esm': {" +
                    "'esm_type': "+ ESM.TYPE_ESM_QUICK_ANSWERS+"," +
                    "'esm_title': 'Sleep quality'," +
                    "'esm_instructions': 'Slept well?'," +
                    "'esm_quick_answers': ['Yes','No']," +
                    "'esm_expiration_threshold': 0," +
                    "'esm_trigger': 'goodmorning_check'" +
                    "}}]";

            Scheduler.Schedule schedule = new Scheduler.Schedule("schedule_id");
            schedule.addHour(9) //0-23
                    .setActionType(Scheduler.ACTION_TYPE_BROADCAST)
                    .setActionIntentAction(ESM.ACTION_AWARE_QUEUE_ESM)
                    .addActionExtra(ESM.EXTRA_ESM, esm_goodmorning);

            Scheduler.saveSchedule(getApplicationContext(), schedule);

            //to remove
            //Scheduler.removeSchedule(getApplicationContext(), "schedule_id");

} catch (JSONException e) {
      e.printStackTrace();
}

Schedule setting the alarm clock when the user plugs the phone to charge at night (21-23)

try{
            Scheduler.Schedule schedule = new Scheduler.Schedule("schedule_id");
            schedule.addHour(21)
                    .addHour(22)
                    .addHour(23)
                    .setContext(Battery.ACTION_AWARE_BATTERY_CHARGING)
                    .setActionType(Scheduler.ACTION_TYPE_ACTIVITY)
                    .setActionClass("com.android.deskclock/com.android.DeskClock");

            Scheduler.saveSchedule(getApplicationContext(), schedule);

            //to remove
            //Scheduler.removeSchedule(getApplicationContext(), "schedule_id");

} catch (JSONException e) {
      e.printStackTrace();
}

Feel free to share your schedules on the discussion board!

 


icon_ios iOS

Please see a tutorial for scheduled ESMs on iOS.

Scheduler