Example usage for android.app AlarmManager set

List of usage examples for android.app AlarmManager set

Introduction

In this page you can find the example usage for android.app AlarmManager set.

Prototype

public void set(@AlarmType int type, long triggerAtMillis, PendingIntent operation) 

Source Link

Document

Schedule an alarm.

Usage

From source file:com.jmstudios.redmoon.receiver.AutomaticFilterChangeReceiver.java

public static void scheduleNextAlarm(Context context, String time, Intent operation) {
    GregorianCalendar calendar = new GregorianCalendar();
    calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(time.split(":")[0]));
    calendar.set(Calendar.MINUTE, Integer.parseInt(time.split(":")[1]));

    GregorianCalendar now = new GregorianCalendar();
    now.add(Calendar.SECOND, 1);/*w w  w.  j a  va  2s.c o  m*/
    if (calendar.before(now)) {
        calendar.add(Calendar.DATE, 1);
    }

    if (DEBUG)
        Log.i(TAG, "Scheduling alarm for " + calendar.toString());

    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, operation, 0);

    if (android.os.Build.VERSION.SDK_INT >= 19) {
        alarmManager.setExact(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
    } else {
        alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
    }
}

From source file:org.isoron.uhabits.helpers.ReminderHelper.java

public static void createReminderAlarm(Context context, Habit habit, @Nullable Long reminderTime) {
    if (!habit.hasReminder())
        return;//  ww  w .jav  a 2 s .c  om

    if (reminderTime == null) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        //noinspection ConstantConditions
        calendar.set(Calendar.HOUR_OF_DAY, habit.reminderHour);
        //noinspection ConstantConditions
        calendar.set(Calendar.MINUTE, habit.reminderMin);
        calendar.set(Calendar.SECOND, 0);

        reminderTime = calendar.getTimeInMillis();

        if (System.currentTimeMillis() > reminderTime)
            reminderTime += AlarmManager.INTERVAL_DAY;
    }

    long timestamp = DateHelper.getStartOfDay(DateHelper.toLocalTime(reminderTime));

    Uri uri = habit.getUri();

    Intent alarmIntent = new Intent(context, HabitBroadcastReceiver.class);
    alarmIntent.setAction(HabitBroadcastReceiver.ACTION_SHOW_REMINDER);
    alarmIntent.setData(uri);
    alarmIntent.putExtra("timestamp", timestamp);
    alarmIntent.putExtra("reminderTime", reminderTime);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
            ((int) (habit.getId() % Integer.MAX_VALUE)) + 1, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    if (Build.VERSION.SDK_INT >= 23)
        manager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, reminderTime, pendingIntent);
    else if (Build.VERSION.SDK_INT >= 19)
        manager.setExact(AlarmManager.RTC_WAKEUP, reminderTime, pendingIntent);
    else
        manager.set(AlarmManager.RTC_WAKEUP, reminderTime, pendingIntent);

    String name = habit.name.substring(0, Math.min(3, habit.name.length()));
    Log.d("ReminderHelper", String.format("Setting alarm (%s): %s",
            DateFormat.getDateTimeInstance().format(new Date(reminderTime)), name));
}

From source file:com.footprint.cordova.plugin.localnotification.LocalNotification.java

/**
 * Set an alarm./* w  ww.jav a2s .  c o  m*/
 *
 * @param options
 *            The options that can be specified per alarm.
 * @param doFireEvent
 *            If the onadd callback shall be called.
 */
public static void add(Options options, boolean doFireEvent) {
    long triggerTime = options.getDate();

    Intent intent = new Intent(context, Receiver.class).setAction("" + options.getId())
            .putExtra(Receiver.OPTIONS, options.getJSONObject().toString());

    AlarmManager am = getAlarmManager();
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

    if (doFireEvent) {
        fireEvent("add", options.getId(), options.getJSON());
    }

    am.set(AlarmManager.RTC_WAKEUP, triggerTime, pi);
}

From source file:org.videolan.vlc.gui.video.AdvOptionsDialog.java

public static void setSleep(Context context, Calendar time) {
    AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(VLCApplication.SLEEP_INTENT);
    PendingIntent sleepPendingIntent = PendingIntent.getBroadcast(VLCApplication.getAppContext(), 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    if (time != null) {
        alarmMgr.set(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), sleepPendingIntent);
    } else {/*from www  .ja v a  2s  . com*/
        alarmMgr.cancel(sleepPendingIntent);
    }
    VLCApplication.sPlayerSleepTime = time;
}

From source file:Main.java

private static long setUpAlarm(AlarmManager alarmManager, PendingIntent pi, long timeInterval) {
    long curTime;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        final AlarmManager.AlarmClockInfo alarmClockInfo = new AlarmManager.AlarmClockInfo(
                (curTime = System.currentTimeMillis()) + timeInterval, pi);
        alarmManager.setAlarmClock(alarmClockInfo, pi);
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, (curTime = System.currentTimeMillis()) + timeInterval,
                pi);//  w  w w  . j  a va  2  s . c om
    } else {
        alarmManager.set(AlarmManager.RTC_WAKEUP, (curTime = System.currentTimeMillis()) + timeInterval, pi);
    }
    return curTime;
}

From source file:Main.java

public static void setAlarm(Context context, Intent intent, int notificationId, Calendar calendar) {
    intent.putExtra("NOTIFICATION_ID", notificationId);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, notificationId, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                pendingIntent);//from  w  w  w .jav a2 s  .  co m
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
    } else {
        alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
    }
}

From source file:com.meiste.greg.ptw.RaceAlarm.java

@SuppressLint("NewApi")
public static void set(final Context context) {
    final Race race = Race.getNext(context, true, true);

    if (!alarm_set && (race != null)) {
        Util.log("Setting race alarm for race " + race.getId());

        final AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        final Intent intent = new Intent(context, RaceAlarm.class);
        intent.putExtra(RACE_ID, race.getId());
        final PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            am.setExact(AlarmManager.RTC_WAKEUP, race.getStartTimestamp(), pendingIntent);
        } else {//  w  ww.  j  av a2 s. c  o  m
            am.set(AlarmManager.RTC_WAKEUP, race.getStartTimestamp(), pendingIntent);
        }

        alarm_set = true;
    } else {
        Util.log("Not setting race alarm: alarm_set=" + alarm_set);
    }
}

From source file:cn.studyjams.s2.sj0132.bowenyan.mygirlfriend.nononsenseapps.notepad.data.receiver.NotificationHelper.java

/**
 * Schedules to be woken up at the next notification time.
 *///  ww w  .  java2s  .  c om
private static void scheduleNext(Context context) {
    // Get first future notification
    final Calendar now = Calendar.getInstance();
    final List<cn.studyjams.s2.sj0132.bowenyan.mygirlfriend.nononsenseapps.notepad.data.model.sql.Notification> notifications = cn.studyjams.s2.sj0132.bowenyan.mygirlfriend.nononsenseapps.notepad.data.model.sql.Notification
            .getNotificationsWithTime(context, now.getTimeInMillis(), false);

    // if not empty, schedule alarm wake up
    if (!notifications.isEmpty()) {
        // at first's time
        // Create a new PendingIntent and add it to the AlarmManager
        Intent intent = new Intent(Intent.ACTION_RUN);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1, intent,
                PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        am.cancel(pendingIntent);
        am.set(AlarmManager.RTC_WAKEUP, notifications.get(0).time, pendingIntent);
    }

    monitorUri(context);
}

From source file:com.granita.tasks.notification.NotificationActionUtils.java

/**
 * Registers a timeout for the undo notification such that when it expires, the undo bar will disappear, and the action will be performed.
 *//*from  w  ww  . j a  v a  2 s  .  co  m*/
public static void registerUndoTimeout(final Context context, final NotificationAction notificationAction) {

    if (sUndoTimeoutMillis == -1) {
        sUndoTimeoutMillis = TIMEOUT_MILLIS;
    }

    final AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    final long triggerAtMills = SystemClock.elapsedRealtime() + sUndoTimeoutMillis;
    final PendingIntent pendingIntent = createUndoTimeoutPendingIntent(context, notificationAction);
    alarmManager.set(AlarmManager.ELAPSED_REALTIME, triggerAtMills, pendingIntent);
}

From source file:com.meiste.greg.ptw.QuestionAlarm.java

@SuppressLint("NewApi")
public static void set(final Context context) {
    // Get next points race: allow in progress
    Race race = Race.getNext(context, false, true);
    if (race == null)
        return;/*from  ww  w  . j  av a 2s.  c  o  m*/

    // Check if user was already reminded of in progress race
    if (Util.getState(context).getInt(LAST_REMIND, -1) >= race.getId()) {
        // Get next points race: do not allow in progress
        race = Race.getNext(context, false, false);
        if (race == null)
            return;
    }

    if (!alarm_set) {
        Util.log("Setting question alarm for race " + race.getId());

        final AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        final Intent intent = new Intent(context, QuestionAlarm.class);
        intent.putExtra(RACE_ID, race.getId());
        final PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            am.setExact(AlarmManager.RTC_WAKEUP, race.getQuestionTimestamp(), pendingIntent);
        } else {
            am.set(AlarmManager.RTC_WAKEUP, race.getQuestionTimestamp(), pendingIntent);
        }

        alarm_set = true;
    } else {
        Util.log("Not setting question alarm: alarm_set=" + alarm_set);
    }
}