Java Code Examples for android.app.AlarmManager#setAndAllowWhileIdle()

The following examples show how to use android.app.AlarmManager#setAndAllowWhileIdle() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AlarmBootReceiver.java    From Amadeus with GNU General Public License v3.0 8 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {

    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);

    if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())
            && settings.getBoolean("alarm_toggle", false)) {
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(
                Context.ALARM_SERVICE);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
                Alarm.ALARM_ID, new Intent(context, AlarmReceiver.class), 0);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            alarmManager.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, settings.getLong("alarm_time", 0), pendingIntent);
        } else {
            alarmManager.set(AlarmManager.RTC_WAKEUP, settings.getLong("alarm_time", 0), pendingIntent);
        }
        Log.d(TAG, "Alarm has been recovered");
    }

}
 
Example 2
Source File: Publisher.java    From PresencePublisher with MIT License 7 votes vote down vote up
private void scheduleFor(long nextSchedule, boolean ignoreBattery) {
    AlarmManager alarmManager = (AlarmManager) applicationContext.getSystemService(Context.ALARM_SERVICE);
    if (alarmManager == null) {
        HyperLog.e(TAG, "Unable to get alarm manager, cannot schedule!");
        return;
    }
    alarmManager.cancel(scheduledIntent);
    HyperLog.i(TAG, "Next run at " + DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(new Date(nextSchedule)));
    sharedPreferences.edit().putLong(NEXT_SCHEDULE, nextSchedule).apply();
    NotificationManagerCompat.from(applicationContext)
            .notify(NOTIFICATION_ID, NotificationFactory.getNotification(applicationContext, lastSuccess, nextSchedule));
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (ignoreBattery) {
            alarmManager.setAlarmClock(new AlarmManager.AlarmClockInfo(nextSchedule, scheduledIntent), scheduledIntent);
        } else {
            alarmManager.setAndAllowWhileIdle(RTC_WAKEUP, nextSchedule, scheduledIntent);
        }
    } else {
        alarmManager.set(RTC_WAKEUP, nextSchedule, scheduledIntent);
    }
}
 
Example 3
Source File: EntityMessage.java    From FairEmail with GNU General Public License v3.0 7 votes vote down vote up
static void snooze(Context context, long id, Long wakeup) {
    Intent snoozed = new Intent(context, ServiceUI.class);
    snoozed.setAction("wakeup:" + id);
    PendingIntent pi = PendingIntent.getService(context, ServiceUI.PI_WAKEUP, snoozed, PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    if (wakeup == null || wakeup == Long.MAX_VALUE) {
        Log.i("Cancel snooze id=" + id);
        am.cancel(pi);
    } else {
        Log.i("Set snooze id=" + id + " wakeup=" + new Date(wakeup));
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
            am.set(AlarmManager.RTC_WAKEUP, wakeup, pi);
        else
            am.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, wakeup, pi);
    }
}
 
Example 4
Source File: BaseStickyService.java    From AcDisplay with GNU General Public License v2.0 6 votes vote down vote up
@SuppressLint("NewApi")
private void remoteRestartService() {
    Timber.d("Remote restart service.");
    final Intent intent = new Intent(this, getClass());
    final PendingIntent pi = PendingIntent.getService(this, code(), intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    // Ask alarm manger to restart us.
    AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    if (Device.hasMarshmallowApi()) {
        alarm.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,
                System.currentTimeMillis() + PENDING_INTENT_RESTART_DELAY,
                pi);
    } else alarm.set(AlarmManager.RTC_WAKEUP,
            System.currentTimeMillis() + PENDING_INTENT_RESTART_DELAY,
            pi);
}
 
Example 5
Source File: ServiceTileMain.java    From tracker-control-android with GNU General Public License v3.0 5 votes vote down vote up
public void onClick() {
    Log.i(TAG, "Click");

    // Cancel set alarm
    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(WidgetAdmin.INTENT_ON);
    intent.setPackage(getPackageName());
    PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    am.cancel(pi);

    // Check state
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    boolean enabled = !prefs.getBoolean("enabled", false);
    prefs.edit().putBoolean("enabled", enabled).apply();
    if (enabled)
        ServiceSinkhole.start("tile", this);
    else {
        ServiceSinkhole.stop("tile", this, false);

        // Auto enable
        int auto = Integer.parseInt(prefs.getString("auto_enable", "0"));
        if (auto > 0) {
            Log.i(TAG, "Scheduling enabled after minutes=" + auto);
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
                am.set(AlarmManager.RTC_WAKEUP, new Date().getTime() + auto * 60 * 1000L, pi);
            else
                am.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, new Date().getTime() + auto * 60 * 1000L, pi);
        }
    }
}
 
Example 6
Source File: ServiceTileMain.java    From NetGuard with GNU General Public License v3.0 5 votes vote down vote up
public void onClick() {
    Log.i(TAG, "Click");

    // Cancel set alarm
    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(WidgetAdmin.INTENT_ON);
    intent.setPackage(getPackageName());
    PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    am.cancel(pi);

    // Check state
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    boolean enabled = !prefs.getBoolean("enabled", false);
    prefs.edit().putBoolean("enabled", enabled).apply();
    if (enabled)
        ServiceSinkhole.start("tile", this);
    else {
        ServiceSinkhole.stop("tile", this, false);

        // Auto enable
        int auto = Integer.parseInt(prefs.getString("auto_enable", "0"));
        if (auto > 0) {
            Log.i(TAG, "Scheduling enabled after minutes=" + auto);
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
                am.set(AlarmManager.RTC_WAKEUP, new Date().getTime() + auto * 60 * 1000L, pi);
            else
                am.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, new Date().getTime() + auto * 60 * 1000L, pi);
        }
    }
}
 
Example 7
Source File: NotificationJobScheduler.java    From Slide with GNU General Public License v3.0 5 votes vote down vote up
public void start(Context c) {
    AlarmManager manager = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);
    int interval = 1000 * 60 * Reddit.notificationTime;
    long currentTime = System.currentTimeMillis();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        manager.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, currentTime + interval, pendingIntent);
    } else {
        manager.set(AlarmManager.RTC_WAKEUP, currentTime + interval, pendingIntent);
    }
}
 
Example 8
Source File: AlarmUtil.java    From react-native-alarm-notification with MIT License 4 votes vote down vote up
void setAlarm(AlarmModel alarm) {
    alarm.setActive(1);
    getAlarmDB().update(alarm);

    Calendar calendar = new GregorianCalendar();
    calendar.set(Calendar.HOUR_OF_DAY, alarm.getHour());
    calendar.set(Calendar.MINUTE, alarm.getMinute());
    calendar.set(Calendar.SECOND, alarm.getSecond());
    calendar.set(Calendar.DAY_OF_MONTH, alarm.getDay());
    calendar.set(Calendar.MONTH, alarm.getMonth() - 1);
    calendar.set(Calendar.YEAR, alarm.getYear());

    Log.e(TAG, alarm.getAlarmId() + " - " + calendar.getTime().toString());

    int alarmId = alarm.getAlarmId();

    Intent intent = new Intent(mContext, AlarmReceiver.class);
    intent.putExtra("intentType", ADD_INTENT);
    intent.putExtra("PendingId", alarm.getId());

    PendingIntent alarmIntent = PendingIntent.getBroadcast(mContext, alarmId, intent, 0);
    AlarmManager alarmManager = this.getAlarmManager();

    String scheduleType = alarm.getScheduleType();

    if (scheduleType.equals("once")) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            alarmManager.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);
        } else {
            alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);
        }
    } else if (scheduleType.equals("repeat")) {
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarm.getInterval() * 1000, alarmIntent);
    } else {
        Log.d(TAG, "Schedule type should either be once or repeat");
        return;
    }

    this.setBootReceiver();
}
 
Example 9
Source File: AlarmUtil.java    From react-native-alarm-notification with MIT License 4 votes vote down vote up
void snoozeAlarm(AlarmModel alarm) {
    Calendar calendar = new GregorianCalendar();
    calendar.set(Calendar.HOUR_OF_DAY, alarm.getHour());
    calendar.set(Calendar.MINUTE, alarm.getMinute());
    calendar.set(Calendar.SECOND, alarm.getSecond());
    calendar.set(Calendar.DAY_OF_MONTH, alarm.getDay());
    calendar.set(Calendar.MONTH, alarm.getMonth() - 1);
    calendar.set(Calendar.YEAR, alarm.getYear());

    // set snooze interval
    calendar.add(Calendar.MINUTE, alarm.getSnoozeInterval());

    alarm.setSecond(calendar.get(Calendar.SECOND));
    alarm.setMinute(calendar.get(Calendar.MINUTE));
    alarm.setHour(calendar.get(Calendar.HOUR_OF_DAY));
    alarm.setDay(calendar.get(Calendar.DAY_OF_MONTH));
    alarm.setMonth(calendar.get(Calendar.MONTH) - 1);
    alarm.setYear(calendar.get(Calendar.YEAR));

    alarm.setAlarmId((int) System.currentTimeMillis());

    getAlarmDB().update(alarm);

    Log.e(TAG, "snooze data - " + alarm.toString());

    int alarmId = alarm.getAlarmId();

    Intent intent = new Intent(mContext, AlarmReceiver.class);
    intent.putExtra("intentType", ADD_INTENT);
    intent.putExtra("PendingId", alarm.getId());

    PendingIntent alarmIntent = PendingIntent.getBroadcast(mContext, alarmId, intent, 0);
    AlarmManager alarmManager = this.getAlarmManager();

    String scheduleType = alarm.getScheduleType();

    if (scheduleType.equals("once")) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            alarmManager.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);
        } else {
            alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);
        }
    } else if (scheduleType.equals("repeat")) {
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarm.getInterval() * 1000, alarmIntent);
    } else {
        Log.d(TAG, "Schedule type should either be once or repeat");
    }
}
 
Example 10
Source File: API23Wrapper.java    From Pedometer with Apache License 2.0 4 votes vote down vote up
public static void setAlarmWhileIdle(AlarmManager am, int type, long time,
                                     PendingIntent intent) {
    am.setAndAllowWhileIdle(type, time, intent);
}