Example usage for android.app AlarmManager setExact

List of usage examples for android.app AlarmManager setExact

Introduction

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

Prototype

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

Source Link

Document

Schedule an alarm to be delivered precisely at the stated time.

Usage

From source file:Main.java

@TargetApi(value = 19)
private static void setAlarmAfter19(AlarmManager am, long timestamp, PendingIntent pendingIntent) {
    am.setExact(AlarmManager.RTC_WAKEUP, timestamp, pendingIntent);
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.KITKAT)
public static void setAlarmExact(Context context, int type, long triggerAtMillis, PendingIntent pendingIntent) {
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        alarmManager.setExact(type, triggerAtMillis, pendingIntent);
    } else {//  www.  j  a v a  2  s  . c om
        alarmManager.set(type, triggerAtMillis, pendingIntent);
    }
}

From source file:Main.java

@SuppressLint("NewApi")
private static void setAlarm(Context context, Calendar calendar, PendingIntent pIntent) {
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pIntent);
    } else {//  w ww .  j a  v a 2  s .  co  m
        alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pIntent);
    }
}

From source file:com.commonsware.android.job.PollReceiver.java

static void scheduleExactAlarm(Context ctxt, AlarmManager alarms, long period, boolean isDownload) {
    Intent i = new Intent(ctxt, PollReceiver.class).putExtra(EXTRA_PERIOD, period).putExtra(EXTRA_IS_DOWNLOAD,
            isDownload);//from  w  ww .  j  a va 2 s  . c o  m
    PendingIntent pi = PendingIntent.getBroadcast(ctxt, 0, i, 0);

    alarms.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + period, pi);
}

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 ww . j  av a2 s .  c  om
    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;/*w  w w. ja  v  a2 s . c  o m*/

    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: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);//ww  w . j  av a2  s  .  com
    } 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);//w  ww.  j a va  2s .  c om
    } 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 {/*from   ww  w  .  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: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   w w  w  . j  a v  a2 s.  co  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);
    }
}