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:nkarasch.repeatingreminder.scheduling.AlertBroadcastReceiver.java

/**
 * Schedules the next firing of this BroadcastReceiver based on the data in the Alert and fires the
 * Alert if appropriate/* ww w  . j a  v  a2  s .c  om*/
 */
@Override
public void onReceive(Context context, Intent intent) {

    Alert alert = intent.getParcelableExtra("alert");
    boolean initial = intent.getBooleanExtra("initial", false);

    intent.putExtra("initial", false);
    Calendar nextTime = getNextTime(Calendar.getInstance(), alert);
    if (nextTime != null) {
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        PendingIntent sender = PendingIntent.getBroadcast(context, alert.getId(), intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        alarmManager.set(AlarmManager.RTC_WAKEUP, nextTime.getTimeInMillis(), sender);
    }

    if (initial) {
        return;
    }

    Calendar calendar = Calendar.getInstance();
    if (!alert.isSchedule()) {
        fireNotification(context, alert);
    } else {
        if (isLegalDay(calendar, alert) && isLegalTime(calendar, alert) == DAY_POSITION.BETWEEN) {
            fireNotification(context, alert);
        }
    }
}

From source file:com.gdgdevfest.android.apps.devfestbcn.service.SessionAlarmService.java

private void scheduleAlarm(final long sessionStart, final long sessionEnd, final long alarmOffset) {

    NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    nm.cancel(NOTIFICATION_ID);//from w w w  .j  av  a  2  s .  c  om
    final long currentTime = UIUtils.getCurrentTime(this);
    // If the session is already started, do not schedule system notification.
    if (currentTime > sessionStart)
        return;

    // By default, sets alarm to go off at 10 minutes before session start time.  If alarm
    // offset is provided, alarm is set to go off by that much time from now.
    long alarmTime;
    if (alarmOffset == UNDEFINED_ALARM_OFFSET) {
        alarmTime = sessionStart - MILLI_TEN_MINUTES;
    } else {
        alarmTime = currentTime + alarmOffset;
    }

    final Intent notifIntent = new Intent(ACTION_NOTIFY_SESSION, null, this, SessionAlarmService.class);
    // Setting data to ensure intent's uniqueness for different session start times.
    notifIntent.setData(new Uri.Builder().authority("com.gdgdevfest.android.apps.devfestbcn")
            .path(String.valueOf(sessionStart)).build());
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_START, sessionStart);
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_END, sessionEnd);
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_ALARM_OFFSET, alarmOffset);
    PendingIntent pi = PendingIntent.getService(this, 0, notifIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    final AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    // Schedule an alarm to be fired to notify user of added sessions are about to begin.
    am.set(AlarmManager.RTC_WAKEUP, alarmTime, pi);
}

From source file:org.sufficientlysecure.keychain.service.PassphraseCacheService.java

/**
 * Executed when service is started by intent
 *///from w  w w  .java  2 s. c  om
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d(TAG, "onStartCommand()");

    // register broadcastreceiver
    registerReceiver();

    if (intent != null && intent.getAction() != null) {
        if (ACTION_PASSPHRASE_CACHE_ADD.equals(intent.getAction())) {
            long ttl = intent.getLongExtra(EXTRA_TTL, DEFAULT_TTL);
            long keyId = intent.getLongExtra(EXTRA_KEY_ID, -1);
            String passphrase = intent.getStringExtra(EXTRA_PASSPHRASE);

            Log.d(TAG, "Received ACTION_PASSPHRASE_CACHE_ADD intent in onStartCommand() with keyId: " + keyId
                    + ", ttl: " + ttl);

            // add keyId and passphrase to memory
            mPassphraseCache.put(keyId, passphrase);

            if (ttl > 0) {
                // register new alarm with keyId for this passphrase
                long triggerTime = new Date().getTime() + (ttl * 1000);
                AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
                am.set(AlarmManager.RTC_WAKEUP, triggerTime, buildIntent(this, keyId));
            }
        } else if (ACTION_PASSPHRASE_CACHE_GET.equals(intent.getAction())) {
            long keyId = intent.getLongExtra(EXTRA_KEY_ID, -1);
            Messenger messenger = intent.getParcelableExtra(EXTRA_MESSENGER);

            String passphrase = getCachedPassphraseImpl(keyId);

            Message msg = Message.obtain();
            Bundle bundle = new Bundle();
            bundle.putString(EXTRA_PASSPHRASE, passphrase);
            msg.obj = bundle;
            try {
                messenger.send(msg);
            } catch (RemoteException e) {
                Log.e(Constants.TAG, "Sending message failed", e);
            }
        } else {
            Log.e(Constants.TAG, "Intent or Intent Action not supported!");
        }
    }

    return START_STICKY;
}

From source file:com.google.android.apps.iosched.service.SessionAlarmService.java

private void scheduleAlarm(final long sessionStart, final long sessionEnd, final long alarmOffset) {

    NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    nm.cancel(NOTIFICATION_ID);//from   www  .  jav  a  2s  .co  m
    final long currentTime = UIUtils.getCurrentTime(this);
    // If the session is already started, do not schedule system notification.
    if (currentTime > sessionStart)
        return;

    // By default, sets alarm to go off at 10 minutes before session start time.  If alarm
    // offset is provided, alarm is set to go off by that much time from now.
    long alarmTime;
    if (alarmOffset == UNDEFINED_ALARM_OFFSET) {
        alarmTime = sessionStart - MILLI_TEN_MINUTES;
    } else {
        alarmTime = currentTime + alarmOffset;
    }

    final Intent notifIntent = new Intent(ACTION_NOTIFY_SESSION, null, this, SessionAlarmService.class);
    // Setting data to ensure intent's uniqueness for different session start times.
    notifIntent.setData(new Uri.Builder().authority("com.google.android.apps.iosched")
            .path(String.valueOf(sessionStart)).build());
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_START, sessionStart);
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_END, sessionEnd);
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_ALARM_OFFSET, alarmOffset);
    PendingIntent pi = PendingIntent.getService(this, 0, notifIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    final AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    // Schedule an alarm to be fired to notify user of added sessions are about to begin.
    am.set(AlarmManager.RTC_WAKEUP, alarmTime, pi);
}

From source file:dk.kk.cykelsuperstier.reminders.AlarmReceiver.java

@Override
public void onReceive(Context context, Intent intent) {

    if (getAbortBroadcast()) {
        return;//  w w w  . java 2  s  . c o m
    }
    int repetition = intent.getExtras().getInt("repetition");
    if (repetition >= 0) {
        // Find next alarm time.
        // If repetition is EVERY_DAY no need to set again, reminder was
        // initially set repetitive.+
        Calendar nextAlarmTime = AlarmUtils.calculateNextAlarmTime(repetition);
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent newIntent = new Intent(AlarmUtils.ALARM_ACTION);
        newIntent.putExtra("repetition", repetition);
        PendingIntent sender = PendingIntent.getBroadcast(context,
                CykelPlanenApplication.ALARM_REQUEST_CODE + 1, newIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        am.set(AlarmManager.RTC_WAKEUP, nextAlarmTime.getTimeInMillis(), sender);
    }

    createNotification(context, repetition);
}

From source file:org.smssecure.smssecure.service.KeyCachingService.java

private void startTimeoutIfAppropriate() {
    boolean timeoutEnabled = SilencePreferences.isPassphraseTimeoutEnabled(this);

    if ((activitiesRunning == 0) && (KeyCachingService.masterSecret != null) && timeoutEnabled
            && !SilencePreferences.isPasswordDisabled(this)) {
        long timeoutMinutes = SilencePreferences.getPassphraseTimeoutInterval(this);
        long timeoutMillis = TimeUnit.MINUTES.toMillis(timeoutMinutes);

        Log.w("KeyCachingService", "Starting timeout: " + timeoutMillis);

        AlarmManager alarmManager = (AlarmManager) this.getSystemService(ALARM_SERVICE);
        alarmManager.cancel(pending);//w  ww  . ja v  a  2 s . c  o m
        alarmManager.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + timeoutMillis, pending);
    }
}

From source file:com.tingtingapps.securesms.service.KeyCachingService.java

private void startTimeoutIfAppropriate() {
    boolean timeoutEnabled = TextSecurePreferences.isPassphraseTimeoutEnabled(this);

    if ((activitiesRunning == 0) && (KeyCachingService.masterSecret != null) && timeoutEnabled
            && !TextSecurePreferences.isPasswordDisabled(this)) {
        long timeoutMinutes = TextSecurePreferences.getPassphraseTimeoutInterval(this);
        long timeoutMillis = TimeUnit.MINUTES.toMillis(timeoutMinutes);

        Log.w("KeyCachingService", "Starting timeout: " + timeoutMillis);

        AlarmManager alarmManager = (AlarmManager) this.getSystemService(ALARM_SERVICE);
        alarmManager.cancel(pending);/*  w w  w .  jav a  2  s. c o  m*/
        alarmManager.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + timeoutMillis, pending);
    }
}

From source file:com.glanznig.beepme.BeeperApp.java

public void setTimer() {
    if (isBeeperActive()) {
        Calendar alarmTime = Calendar.getInstance();
        Calendar alarmTimeUTC = Calendar.getInstance(TimeZone.getTimeZone("UTC"));

        if (timerProfile == null) {
            setTimerProfile();//from w  w w  . j  a v  a 2  s.c om
        }

        long timer = timerProfile.getTimer(this.getApplicationContext());
        alarmTime.add(Calendar.SECOND, (int) timer);
        //Log.i(TAG, "alarm in " + timer + " seconds.");
        alarmTimeUTC.add(Calendar.SECOND, (int) timer);
        getPreferences().setScheduledBeepId(new ScheduledBeepTable(this.getApplicationContext())
                .addScheduledBeep(alarmTime.getTimeInMillis(), getPreferences().getUptimeId()));

        Intent intent = new Intent(this, BeepActivity.class);
        PendingIntent alarmIntent = PendingIntent.getActivity(this, ALARM_INTENT_ID, intent,
                PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager manager = (AlarmManager) getSystemService(Activity.ALARM_SERVICE);
        manager.set(AlarmManager.RTC_WAKEUP, alarmTimeUTC.getTimeInMillis(), alarmIntent);
    }
}

From source file:com.ivarprudnikov.sensors.OnBootBroadcastReceiver.java

@Override
public void onReceive(Context context, Intent intent) {

    AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    // Starts service responsible for sensor data storage
    Intent startSensorDataServiceIntent = new Intent(context, OnAlarmBroadcastReceiver.class);
    startSensorDataServiceIntent.setAction(Constants.INTENT_ACTION_TRIGGER_FROM_BOOT);
    PendingIntent pi1 = PendingIntent.getBroadcast(context, 0, startSensorDataServiceIntent, 0);
    mgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + START_AT_OFFSET, pi1);

    // Registers alarms required for data export
    Intent startDataExportIntent = new Intent(context, OnExportAlarmBroadcastReceiver.class);
    startDataExportIntent.setAction(Constants.INTENT_ACTION_TRIGGER_FROM_BOOT);
    PendingIntent pi2 = PendingIntent.getBroadcast(context, 0, startSensorDataServiceIntent, 0);
    mgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + START_AT_OFFSET, pi2);
}

From source file:com.spoiledmilk.cykelsuperstier.reminders.AlarmReceiver.java

@Override
public void onReceive(Context context, Intent intent) {

    if (getAbortBroadcast()) {
        return;// w  w  w .  ja va 2  s  .c  om
    }
    int repetition = intent.getExtras().getInt("repetition");
    if (repetition >= 0) {
        // Find next alarm time.
        // If repetition is EVERY_DAY no need to set again, reminder was
        // initially set repetitive.+
        Calendar nextAlarmTime = AlarmUtils.calculateNextAlarmTime(repetition);
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent newIntent = new Intent(AlarmUtils.ALARM_ACTION);
        newIntent.putExtra("repetition", repetition);
        PendingIntent sender = PendingIntent.getBroadcast(context,
                CykelsuperstierApplication.ALARM_REQUEST_CODE + 1, newIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        am.set(AlarmManager.RTC_WAKEUP, nextAlarmTime.getTimeInMillis(), sender);
    }

    createNotification(context, repetition);
}