Example usage for android.app AlarmManager setWindow

List of usage examples for android.app AlarmManager setWindow

Introduction

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

Prototype

public void setWindow(@AlarmType int type, long windowStartMillis, long windowLengthMillis,
        PendingIntent operation) 

Source Link

Document

Schedule an alarm to be delivered within a given window of time.

Usage

From source file:Main.java

public static void setAlarmTime(Context context, long timeInMillis, Intent intent) {
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    PendingIntent sender = PendingIntent.getBroadcast(context, intent.getIntExtra("id", 0), intent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    int interval = (int) intent.getLongExtra("intervalMillis", 0);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        am.setWindow(AlarmManager.RTC_WAKEUP, timeInMillis, interval, sender);
    }/*from   w w  w .j a  v a  2 s.  c om*/
}

From source file:org.dmfs.tasks.notification.NotificationUpdaterService.java

@TargetApi(Build.VERSION_CODES.KITKAT)
private void updateNextDayAlarm() {
    Intent intent = new Intent(this, NotificationUpdaterService.class);
    intent.setAction(ACTION_NEXT_DAY);/*from w ww  .  ja v  a 2  s. co m*/
    mDateChangePendingIntent = PendingIntent.getService(this, 0, intent, 0);

    // set alarm to update the next day
    GregorianCalendar tomorrow = new GregorianCalendar();
    tomorrow.add(Calendar.DAY_OF_YEAR, 1);
    tomorrow.set(Calendar.HOUR_OF_DAY, 0);
    tomorrow.set(Calendar.MINUTE, 0);
    tomorrow.set(Calendar.SECOND, 0);
    tomorrow.set(Calendar.MILLISECOND, 0);

    AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
    if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
        alarmManager.setWindow(AlarmManager.RTC, tomorrow.getTimeInMillis(), 1000, mDateChangePendingIntent);
    } else {
        alarmManager.set(AlarmManager.RTC, tomorrow.getTimeInMillis(), mDateChangePendingIntent);
    }
}

From source file:com.lambdasoup.quickfit.alarm.AlarmService.java

/**
 * sets the alarm with the alarm manager for the next occurrence of any scheduled event according
 * to the current db state/*w w w  . j  ava  2s  .c  o m*/
 */
@WorkerThread
private void setNextAlarm() {
    try (SQLiteDatabase db = dbHelper.getReadableDatabase();
            Cursor cursor = db.rawQuery(QUERY_SELECT_MIN_NEXT_ALERT, null)) {
        // if cursor is empty, no schedules exist, no alarms to set
        if (cursor.moveToFirst()) {
            long nextAlarmMillis = cursor
                    .getLong(cursor.getColumnIndexOrThrow(ScheduleEntry.COL_NEXT_ALARM_MILLIS));

            AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
            PendingIntent alarmReceiverIntent = PendingIntent.getBroadcast(this, PENDING_INTENT_ALARM_RECEIVER,
                    AlarmReceiver.getIntentOnAlarm(this), PendingIntent.FLAG_UPDATE_CURRENT);
            alarmManager.cancel(alarmReceiverIntent);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, nextAlarmMillis,
                        alarmReceiverIntent);
            } else {
                alarmManager.setWindow(AlarmManager.RTC_WAKEUP, nextAlarmMillis, DateUtils.MINUTE_IN_MILLIS,
                        alarmReceiverIntent);
            }
        }
    }
}

From source file:com.actinarium.nagbox.service.NagboxService.java

private void rescheduleAlarm() {
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    // Prepare pending intent. Setting, updating, or cancelling the alarm - we need it in either case
    Intent intent = new Intent(this, NagAlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    long nextTimestamp = NagboxDbOps.getClosestNagTimestamp(mDatabase);
    if (nextTimestamp == 0) {
        alarmManager.cancel(pendingIntent);
    } else {//from   w  w  w  . ja va2  s  .c om
        // todo: deal with exact/inexact reminders later
        if (Build.VERSION.SDK_INT >= 23) {
            alarmManager.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, nextTimestamp, pendingIntent);
        } else if (Build.VERSION.SDK_INT >= 19) {
            alarmManager.setWindow(AlarmManager.RTC_WAKEUP, nextTimestamp, ALARM_TOLERANCE, pendingIntent);
        } else {
            alarmManager.set(AlarmManager.RTC_WAKEUP, nextTimestamp, pendingIntent);
        }
    }
}

From source file:org.microg.gms.gcm.McsService.java

public void scheduleHeartbeat(Context context) {
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);

    int heartbeatMs = GcmPrefs.get(this).getHeartbeatMsFor(activeNetworkPref, false);
    if (heartbeatMs < 0) {
        closeAll();/*  w w  w .  j av  a  2 s.  c om*/
    }
    logd("Scheduling heartbeat in " + heartbeatMs / 1000 + " seconds...");
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
        alarmManager.set(ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + heartbeatMs, heartbeatIntent);
    } else {
        // with KitKat, the alarms become inexact by default, but with the newly available setWindow we can get inexact alarms with guarantees.
        // Schedule the alarm to fire within the interval [heartbeatMs/3*4, heartbeatMs]
        alarmManager.setWindow(ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + heartbeatMs / 4 * 3,
                heartbeatMs / 4, heartbeatIntent);
    }

}