Example usage for android.app AlarmManager setExactAndAllowWhileIdle

List of usage examples for android.app AlarmManager setExactAndAllowWhileIdle

Introduction

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

Prototype

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

Source Link

Document

Like #setExact(int,long,PendingIntent) , but this alarm will be allowed to execute even when the system is in low-power idle modes.

Usage

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

public static void createReminderAlarm(Context context, Habit habit, @Nullable Long reminderTime) {
    if (!habit.hasReminder())
        return;//from   w w w .  j av  a 2s .  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

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);/* www  . j av  a  2 s .  com*/
    } 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.lloydtorres.stately.push.TrixHelper.java

/**
 * Sets an alarm for Alphys to query NS for new notices. The alarm time is on whatever the user
 * selected in settings, starting from the time the function was called. A "jitter" of up to
 * 5 minutes is added on top to prevent overwhelming the NS servers.
 * @param c App context/*from  ww w  .java  2  s  . c  o m*/
 */
public static void setAlarmForAlphys(Context c) {
    // First check if alarms should be set to begin with.
    if (!SettingsActivity.getNotificationSetting(c)) {
        return;
    }

    Intent alphysIntent = new Intent(c, AlphysReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(c, 0, alphysIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    long timeToNextAlarm = System.currentTimeMillis()
            + SettingsActivity.getNotificationIntervalSetting(c) * 1000L;
    // add "jitter" from 0 min to 5 min to next alarm to prevent overwhelming NS servers
    Random r = new Random();
    timeToNextAlarm += (long) (r.nextDouble() * FIVE_MIN_IN_MS);

    // Source:
    // https://www.reddit.com/r/Android/comments/44opi3/reddit_sync_temporarily_blocked_for_bad_api_usage/czs3ne4
    AlarmManager am = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, timeToNextAlarm, pendingIntent);
    } else if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        am.setExact(AlarmManager.RTC_WAKEUP, timeToNextAlarm, pendingIntent);
    } else {
        am.set(AlarmManager.RTC_WAKEUP, timeToNextAlarm, pendingIntent);
    }
}

From source file:com.androidinspain.deskclock.data.TimerModel.java

static void schedulePendingIntent(AlarmManager am, long triggerTime, PendingIntent pi) {
    if (Utils.isMOrLater()) {
        // Ensure the timer fires even if the device is dozing.
        am.setExactAndAllowWhileIdle(ELAPSED_REALTIME_WAKEUP, triggerTime, pi);
    } else {/*from  www  .ja  va 2s.co  m*/
        am.setExact(ELAPSED_REALTIME_WAKEUP, triggerTime, pi);
    }
}

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.  ja v a  2  s  .c om
 */
@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:xyz.lebalex.lockscreen.MainActivity.java

private void startBackgroundService(int interval, int startTime) {
    try {/*w ww  .j av  a 2s  .c  om*/
        Intent alarmIntent = new Intent(this, LockScreenServiceReceiver.class);
        alarmIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
        PendingIntent pendingIntent;
        pendingIntent = PendingIntent.getBroadcast(this, 1001, alarmIntent, PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        if (interval > 0) {
            Calendar curentTime = Calendar.getInstance();

            Calendar startCalen = Calendar.getInstance();
            startCalen.set(Calendar.HOUR_OF_DAY, startTime);
            startCalen.set(Calendar.MINUTE, 5);
            startCalen.set(Calendar.SECOND, 0);
            startCalen.set(Calendar.MILLISECOND, 0);

            boolean find = false;
            while (!find) {
                if (curentTime.before(startCalen))
                    find = true;
                else
                    startCalen.add(Calendar.MILLISECOND, interval);
            }

            //manager.setRepeating(AlarmManager.RTC_WAKEUP, startCalen.getTimeInMillis(), interval, pendingIntent);
            manager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, startCalen.getTimeInMillis(),
                    pendingIntent);

            SharedPreferences.Editor editor = sp.edit();
            editor.putBoolean("start_service", true);
            editor.commit();
            LogWrite.Log(this,
                    "start Alarm " + startCalen.get(Calendar.YEAR) + "-" + startCalen.get(Calendar.MONTH) + "-"
                            + startCalen.get(Calendar.DATE) + " " + startCalen.get(Calendar.HOUR_OF_DAY) + ":"
                            + startCalen.get(Calendar.MINUTE) + ":" + startCalen.get(Calendar.SECOND));
        } else {
            LogWrite.Log(this, "stop Alarm");
        }
    } catch (Exception e) {
        LogWrite.LogError(this, e.getMessage());
    }
}