Set alarm at certain time of a day - Android Android OS

Android examples for Android OS:Alarm Set Up

Description

Set alarm at certain time of a day

Demo Code

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

public class Main {
  private static final int ALARM_REQUEST = 105170545;
  private static final String INTENT_FILTER_NAME = "com.java2s.alarm.message";

  public static void alarm(Context context) {
    Intent intent = new Intent(context, Main.class);
    PendingIntent sender = PendingIntent.getBroadcast(context, ALARM_REQUEST, new Intent(INTENT_FILTER_NAME),
        PendingIntent.FLAG_CANCEL_CURRENT);
    try {//from w  w w.  j a v a  2 s .c o  m
      final Calendar c = Calendar.getInstance();
      String sCurrentDay = c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1) + "-"
          + c.get(Calendar.DAY_OF_MONTH);

      Date tomorrow = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(sCurrentDay + " 09:00:00");
      AlarmManager am = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
      am.setInexactRepeating(AlarmManager.RTC, tomorrow.getTime(), 24 * 60 * 60 * 1000, sender);
    } catch (ParseException e) {
      e.printStackTrace();
    }
  }
}

Related Tutorials