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:at.ac.uniklu.mobile.sportal.service.MutingService.java

/**
 * Schedules an alarm through the AlarmManager. Alarms are typically scheduled at time when courses begin or end.
 * @param time the time at which the alarm will go off
 * @param action the action that will be called when the alarm goes off
 *//*from  w w  w.  jav  a2s. co m*/
private void scheduleAlarm(long time, int action, int alarmId) {
    Log.d(TAG, "scheduling alarm action " + action + " @ " + new Date(time).toLocaleString() + " (aID:"
            + alarmId + ")");

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    Intent alarmIntent = new Intent(this, OnAlarmReceiver.class).putExtra(ACTION, action)
            .putExtra(EXTRA_ALARM_ID, alarmId);
    PendingIntent pendingAlarmIntent = PendingIntent.getBroadcast(this, 0, alarmIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    time = SystemClock.elapsedRealtime() + (time - System.currentTimeMillis()); //  convert unixtime to system runtime
    alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, time, pendingAlarmIntent);
}

From source file:no.firestorm.weathernotificatonservice.WeatherNotificationService.java

/**
 * Set alarm/*from w  ww .  j a v a  2  s  .  co m*/
 * 
 * @param updateRate
 *            in minutes
 */
private void setAlarm(long updateRate) {
    // Set alarm
    final PendingIntent pendingIntent = PendingIntent.getService(this, 0,
            new Intent(this, WeatherNotificationService.class), PendingIntent.FLAG_UPDATE_CURRENT);
    final AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    // Set time to next hour plus 5 minutes:
    final long now = System.currentTimeMillis();
    long triggerAtTime = now;
    // set back to last hour plus 2 minutes:
    triggerAtTime -= triggerAtTime % 3600000 - 12000;
    // Add selected update rate
    triggerAtTime += updateRate * 60000;

    // Check that trigger time is not passed.
    if (triggerAtTime < now)
        triggerAtTime = now + updateRate * 60000;

    alarm.set(AlarmManager.RTC, triggerAtTime, pendingIntent);
}

From source file:net.imatruck.betterweather.BetterWeatherExtension.java

/**
 * Schedule an update with a {@link android.app.PendingIntent}
 *
 * @param intervalOverride Override in minutes for the next refresh, if 0 use settings value
 *//*from  w ww.ja  v a 2  s.c om*/
private void scheduleRefresh(int intervalOverride) {
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
    sRefreshInterval = Integer.parseInt(sp.getString(PREF_WEATHER_REFRESH_INTERVAL, "60"));
    if (sRefreshInterval < 0) {
        WeatherRefreshReceiver.cancelPendingIntent(this);
        return;
    }

    int realRefreshInterval = sRefreshInterval;
    if (intervalOverride > 0)
        realRefreshInterval = intervalOverride;

    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
    am.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + (realRefreshInterval * 60 * 1000),
            WeatherRefreshReceiver.getPendingIntent(this));
    LOGD(TAG, "Scheduled refresh in " + realRefreshInterval + " minutes.");
}

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();/*from www  . ja v a2  s .  c  o m*/
    }
    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);
    }

}

From source file:com.magnet.mmx.client.MMXClient.java

private static void scheduleWakeupAlarm(Context context, long interval) {
    if (Log.isLoggable(TAG, Log.DEBUG)) {
        Log.d(TAG, "scheduleWakeupAlarm(): called with interval=" + interval);
    }//from ww w  .  ja v  a  2 s .  c om
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    if (interval > 0) {
        //a time was set
        alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + interval,
                getWakeupIntent(context));
    } else {
        //cancel
        if (Log.isLoggable(TAG, Log.DEBUG)) {
            Log.d(TAG, "scheduleWakeupAlarm(): cancelling alarm");
        }
        alarmManager.cancel(getWakeupIntent(context));
    }
}

From source file:com.rickendirk.rsgwijzigingen.ZoekService.java

private void setAlarmIn20Min() {
    Intent zoekIntent = new Intent(this, ZoekService.class);
    zoekIntent.putExtra("isAchtergrond", true);
    zoekIntent.addCategory("GeenWifiHerhaling"); //Categorie om andere intents cancelen te voorkomen
    PendingIntent pendingIntent = PendingIntent.getService(this, 3, zoekIntent, PendingIntent.FLAG_ONE_SHOT);
    AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Long in20Min = SystemClock.elapsedRealtime() + 1200000; //20Min in milisec.
    manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, in20Min, pendingIntent);
    Log.i(TAG, "Nieuw alarm gezet in 20 min");
}

From source file:com.brayanarias.alarmproject.receiver.AlarmReceiver.java

private static void setAlarm(Context context, Alarm alarm) {
    try {/*from w w w.ja  va  2 s . c o  m*/
        PendingIntent pendingIntent = createPendingIntent(context, alarm);
        AlarmManager alarmManager = getAlarmManager(context);
        Calendar actual = Calendar.getInstance();
        actual.set(Calendar.SECOND, 0);
        actual.set(Calendar.MILLISECOND, 0);
        Calendar calendar = (Calendar) actual.clone();
        int ampm = alarm.getAmPm().equals("AM") ? Calendar.AM : Calendar.PM;
        if (alarm.getHour() == 12) {
            calendar.set(Calendar.HOUR, 0);
        } else {
            calendar.set(Calendar.HOUR, alarm.getHour());
        }
        calendar.set(Calendar.AM_PM, ampm);
        calendar.set(Calendar.MINUTE, alarm.getMinute());
        int day = Calendar.getInstance().get(Calendar.DAY_OF_WEEK);
        if (actual.getTimeInMillis() >= calendar.getTimeInMillis()) {
            if (alarm.getDaysOfAlamr().equals("0000000")) {
                calendar.add(Calendar.DATE, 1);
            } else {
                calendar.add(Calendar.DATE, getDaysFromNextAlarm(alarm.getDaysOfAlamr()));
            }
        } else {
            if (!AlarmUtilities.isToday(alarm.getDaysOfAlamr(), day)) {
                calendar.add(Calendar.DATE, getDaysFromNextAlarm(alarm.getDaysOfAlamr()));
            }
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            AlarmManager.AlarmClockInfo clockInfo = new AlarmManager.AlarmClockInfo(calendar.getTimeInMillis(),
                    pendingIntent);
            alarmManager.setAlarmClock(clockInfo, pendingIntent);
        } else if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
            alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
        } else {
            alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
        }

    } catch (Exception e) {
        Log.e(tag, e.getLocalizedMessage(), e);
    }
}

From source file:com.geotrackin.gpslogger.GpsLoggingService.java

/**
 * Sets up the auto email timers based on user preferences.
 *///from   ww w  .  j av a 2s .c o  m
public void SetupAutoSendTimers() {
    tracer.debug(
            "Setting up autosend timers. Auto Send Enabled - " + String.valueOf(AppSettings.isAutoSendEnabled())
                    + ", Auto Send Delay - " + String.valueOf(Session.getAutoSendDelay()));

    if (AppSettings.isAutoSendEnabled() && Session.getAutoSendDelay() > 0) {
        tracer.debug("Setting up autosend alarm");
        long triggerTime = System.currentTimeMillis() + (long) (Session.getAutoSendDelay() * 60 * 60 * 1000);

        alarmIntent = new Intent(getApplicationContext(), AlarmReceiver.class);
        CancelAlarm();

        PendingIntent sender = PendingIntent.getBroadcast(this, 0, alarmIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, triggerTime, sender);
        tracer.debug("Autosend alarm has been set");

    } else {
        if (alarmIntent != null) {
            tracer.debug("alarmIntent was null, canceling alarm");
            CancelAlarm();
        }
    }
}

From source file:com.android.launcher3.widget.DigitalAppWidgetProvider.java

/**
 * Start an alarm that fires on the next quarter hour to update the world clock city
 * day when the local time or the world city crosses midnight.
 *
 * @param context The context in which the PendingIntent should perform the broadcast.
 *///from   w ww .  jav  a  2  s  .co  m
private void startAlarmOnQuarterHour(Context context) {
    if (context != null) {
        long onQuarterHour = Utils.getAlarmOnQuarterHour();
        PendingIntent quarterlyIntent = getOnQuarterHourPendingIntent(context);
        AlarmManager alarmManager = ((AlarmManager) context.getSystemService(Context.ALARM_SERVICE));
        if (Utils.isKitKatOrLater()) {
            alarmManager.setExact(AlarmManager.RTC, onQuarterHour, quarterlyIntent);
        } else {
            alarmManager.set(AlarmManager.RTC, onQuarterHour, quarterlyIntent);
        }
    }
}