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:com.tealeaf.PushBroadcastReceiver.java

public static void scheduleNext(Context context, int timeout) {
    TeaLeafOptions options = new TeaLeafOptions(context);
    String appID = options.getAppID();
    logger.log("{push} Scheduling the next push for", timeout);
    Intent intent = new Intent("com.tealeaf.CHECK_PUSH_SERVER");
    intent.putExtra("appID", appID);
    AlarmManager alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    scheduledIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
    alarms.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (timeout * 1000), scheduledIntent);
}

From source file:Main.java

@SuppressLint("NewApi")
private static void setAlarm(Context context, Calendar calendar, PendingIntent pIntent) {
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pIntent);
    } else {//from   w w  w  . j  a  v a  2  s  .  com
        alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pIntent);
    }
}

From source file:com.kubotaku.android.code4kyoto5374.util.AlarmService.java

private static void setupAlarmOld(Context context, PendingIntent intent, final long triggerAtMillis) {
    final AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC, triggerAtMillis, intent);
}

From source file:com.groundupworks.wings.core.WingsService.java

/**
 * Schedules an alarm to start the {@link WingsService}.
 *
 * @param context the {@link Context}.//from  w w w .j a  v a  2s .c o m
 * @param delay   how far in the future to schedule the alarm.
 */
static void scheduleWingsService(Context context, long delay) {
    Context appContext = context.getApplicationContext();

    // Create pending intent.
    Intent intent = new Intent(appContext, AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(appContext, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    // Set alarm.
    AlarmManager alarmManager = (AlarmManager) appContext.getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, delay, pendingIntent);
}

From source file:Main.java

public static boolean setAlarmByBC(Context context, Class<?> bc_class, int period) {
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    if (alarmManager == null)
        return false;
    long triggerAtTime = SystemClock.elapsedRealtime() + period;
    Intent i = new Intent(context, bc_class);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
    alarmManager.set(AlarmManager.ELAPSED_REALTIME, triggerAtTime, pi);
    return true;//w  w w  .  ja  v  a  2s.  c  om
}

From source file:br.ajmarques.cordova.plugin.localnotification.LocalNotification.java

/**
 * Set an alarm.//from ww w  .  j  av a  2s.  c o m
 *
 * @param options
 *            The options that can be specified per alarm.
 */
public static void add(Options options) {
    long triggerTime = options.getDate();

    Intent intent = new Intent(context, Receiver.class).setAction("" + options.getId())
            .putExtra(Receiver.OPTIONS, options.getJSONObject().toString());

    AlarmManager am = getAlarmManager();
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

    am.set(AlarmManager.RTC_WAKEUP, triggerTime, pi);
}

From source file:se.erichansander.retrotimer.RetroTimer.java

/**
 * Sets an alarm at absolute time alarmTime (in millis from epoch)
 *///from  ww  w.jav a  2s.co  m
public static void setAlarmAt(Context context, long alarmTime) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor ed = prefs.edit();
    ed.putLong(RetroTimer.PREF_ALARM_TIME, alarmTime);
    ed.putBoolean(RetroTimer.PREF_ALARM_SET, true);
    ed.commit();

    Intent intent = new Intent(RetroTimer.ALARM_TRIGGER_ACTION);
    intent.putExtra(RetroTimer.ALARM_TIME_EXTRA, alarmTime);
    PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

    /*
     * Activate alarm in AlarmManager. Since the API for activating the
     * alarm changed in KitKat, we need to treat the two versions
     * differently, and since "Dalvik in Android 1.x was very conservative
     * and would crash if you try to load a class that contains a reference
     * that it cannot resolve" we have to put it in a separate class...
     * 
     * See
     * http://stackoverflow.com/questions/13444255/could-not-find-method-
     * from-the-newer-api-with-using-targetapi-annotation
     */
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
        am.set(AlarmManager.RTC_WAKEUP, alarmTime, sender);
    } else {
        AlarmManagerKitKat.set(am, alarmTime, sender);
    }

    // Trigger a notification that, when clicked, will open TimerSet
    Intent viewAlarm = new Intent(context, TimerSet.class);
    viewAlarm.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pendingNotify = PendingIntent.getActivity(context, 0, viewAlarm, 0);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
            .setContentIntent(pendingNotify).setDefaults(Notification.DEFAULT_LIGHTS).setOngoing(true)
            .setSmallIcon(R.drawable.ic_stat_alarm_set)
            .setContentTitle(context.getString(R.string.notify_set_label)).setContentText(context
                    .getString(R.string.notify_set_text, DateFormat.getTimeFormat(context).format(alarmTime)));

    /*
     * Send the notification using the alarm id to easily identify the
     * correct notification.
     */
    NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    nm.notify(RetroTimer.NOTIF_SET_ID, mBuilder.build());
}

From source file:com.cyanogenmod.account.util.CMAccountUtils.java

public static void scheduleRetry(Context context, SharedPreferences prefs, Intent intent) {
    int backoffTimeMs = getBackoff(prefs);
    int nextAttempt = backoffTimeMs / 2 + sRandom.nextInt(backoffTimeMs);
    if (CMAccount.DEBUG)
        Log.d(TAG, "Scheduling retry, backoff = " + nextAttempt + " (" + backoffTimeMs + ") for "
                + intent.getAction());// w w w .  ja va2 s  .  c  om
    PendingIntent retryPendingIntent = PendingIntent.getService(context, 0, intent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    am.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + nextAttempt, retryPendingIntent);
    if (backoffTimeMs < CMAccount.MAX_BACKOFF_MS) {
        setBackoff(prefs, backoffTimeMs * 2);
    }
}

From source file:com.phonemetra.account.util.AccountUtils.java

public static void scheduleRetry(Context context, SharedPreferences prefs, Intent intent) {
    int backoffTimeMs = getBackoff(prefs);
    int nextAttempt = backoffTimeMs / 2 + sRandom.nextInt(backoffTimeMs);
    if (Account.DEBUG)
        Log.d(TAG, "Scheduling retry, backoff = " + nextAttempt + " (" + backoffTimeMs + ") for "
                + intent.getAction());/*from ww  w.j a  v  a2 s.c o m*/
    PendingIntent retryPendingIntent = PendingIntent.getService(context, 0, intent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    am.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + nextAttempt, retryPendingIntent);
    if (backoffTimeMs < Account.MAX_BACKOFF_MS) {
        setBackoff(prefs, backoffTimeMs * 2);
    }
}

From source file:org.leopub.mat.service.MessageService.java

public static void setUpdate(int latency, int period) {
    Context context = MyApplication.getAppContext();
    Logger.i(TAG, "setUpdate latency:" + latency + ", period:" + period);
    Intent i = new Intent(context, MessageService.class);
    PendingIntent pi = PendingIntent.getService(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);

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

    if (period > 0) {
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + latency * 60 * 1000,
                period * 60 * 1000, pi);
    } else {//from  w  w  w.  j a v  a2  s.c o m
        alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + latency * 60 * 1000, pi);
    }
    Logger.i(TAG, "setUpdate Done");
}