SoFunction
Updated on 2025-04-06

How to set reminder events on Android programming

This article describes the method of setting reminder events in Android programming. Share it for your reference, as follows:

1. Start the service

Intent intent = new Intent(this,);
("reminder_event", reminderModel);
startService(intent);

2、service file

public class AutoTaskService extends Service {
  private ReminderModel mReminderModel = null;
  @Override
  public IBinder onBind(Intent intent) {
    return null;
  }
  @Override
  public void onCreate() {
    ();
  }
  @Override
  public void onDestroy() {
    ();
  }
  @Override
  public void onStart(Intent intent, int startId) {
    if(intent == null)
      return;
    if(("reminder_event")) {
      mReminderModel = (ReminderModel) ("reminder_event");
    }
    setReminderEvent();
    (intent, startId);
  }
  /**
   * set reminder event
   */
  private void setReminderEvent() {
    if(mReminderModel == null)
      return;
    if(())
      return;
    if(())
      return;
    Calendar cal = getCalendarFromDate();
    String[] array = ("-");
    for(int i = 0; i < ; i++) {
      if(i == 0) {
        (Calendar.HOUR_OF_DAY, (array[0]));
      } else if (i == 1) {
        (, (array[1]));
      }
    }
    (, 0);
    Intent intent = new Intent(, );
    if(!()) {
      ("reminder_pic_path", );
    }
    PendingIntent pi = (this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
   // Get AlarmManager object    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
   // AlarmManager.RTC_WAKEUP will run when sleeping, and if so, it will not run when sleeping    (AlarmManager.RTC_WAKEUP, (), pi);
  }
  /**
   * @param date format is 2012-9-18
   * @return Calendar value is after set date's value
   */
  private Calendar getCalendarFromDate(final String date) {
    int year = 0;
    int month = 0;
    int day = 0;
    try {
      String[] array = ("-");
      int[] arrayInt = new int[];
      for (int i = 0; i < ; i++) {
        arrayInt[i] = (array[i]);
        if(i == 0) {
          year = arrayInt[0];
        } else if(i == 1){
          month = arrayInt[1];
        } else if(i == 2){
          day = arrayInt[2];
        }
      }
    } catch (Exception e) {
      ();
    }
    Calendar cal = ();
    if(year > 0 && month >= 0 && day >= 0) {
      (year, month - 1, day);
    }
    return cal;
  }
}

3. Timed receiver

public class AlarmReceiver extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {
    String path = null;
    if(("reminder_pic_path")) {
      path = ("reminder_pic_path");
    }
    ("======> AlarmReceiver", path);
    // Start notification activity    Intent it = new Intent(context, );
    (Intent.FLAG_ACTIVITY_NEW_TASK);
    if(!(path)) {
      ("reminder_pic_path", path);
    }
    (it);
  }
}

I hope this article will be helpful to everyone's Android programming design.