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:de.ub0r.android.websms.WebSMSReceiver.java

/**
 * Schedules resend of a message.// w w  w  . j  a va  2s .  co m
 *
 * @param context context
 * @param specs   {@link de.ub0r.android.websms.connector.common.ConnectorSpec}
 * @param command {@link de.ub0r.android.websms.connector.common.ConnectorCommand}
 */
private static void scheduleMessageResend(final Context context, final ConnectorSpec specs,
        final ConnectorCommand command) {

    long msgId = command.getMsgId();

    final Intent resendIntent = new Intent(Connector.ACTION_RESEND);
    command.setToIntent(resendIntent);
    specs.setToIntent(resendIntent);

    AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + RESEND_DELAY_MS,
            PendingIntent.getBroadcast(context, (int) msgId, resendIntent, PendingIntent.FLAG_CANCEL_CURRENT));
}

From source file:com.google.android.apps.santatracker.notification.SantaNotificationBuilder.java

/**
 * Schedule a basic notification at an approximate time.
 *//*from   w  ww  .  j  a  v a 2  s .  com*/
public static void ScheduleSantaNotification(Context c, long timestamp, int notificationType) {

    // Only schedule a notification if the time is in the future
    if (timestamp < System.currentTimeMillis()) {
        return;
    }

    AlarmManager alarm = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);

    Intent i = new Intent(c, NotificationBroadcastReceiver.class);
    i.putExtra(NotificationConstants.KEY_NOTIFICATION_ID, NotificationConstants.NOTIFICATION_ID);

    // Type is "takeoff", "location", etc.
    i.putExtra(NotificationConstants.KEY_NOTIFICATION_TYPE, notificationType);

    // Generate unique pending intent
    PendingIntent pi = PendingIntent.getBroadcast(c, notificationType, i, 0);

    // Deliver next time the device is woken up
    alarm.set(AlarmManager.RTC, timestamp, pi);

}

From source file:com.google.android.apps.santatracker.SantaNotificationBuilder.java

public static void ScheduleSantaNotification(Context c, long timestamp, int notificationType) {

    // Only schedule a notification if the time is in the future
    if (timestamp < System.currentTimeMillis()) {
        return;/*w  ww  . j av  a2  s .c om*/
    }

    AlarmManager alarm = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);

    Intent i = new Intent(c, NotificationBroadcastReceiver.class);
    i.putExtra(NotificationConstants.KEY_NOTIFICATION_ID, NotificationConstants.NOTIFICATION_ID);

    // Type is "takeoff", "location", etc.
    i.putExtra(NotificationConstants.KEY_NOTIFICATION_TYPE, notificationType);

    // Generate unique pending intent
    PendingIntent pi = PendingIntent.getBroadcast(c, notificationType, i, 0);

    // Deliver next time the device is woken up
    alarm.set(AlarmManager.RTC, timestamp, pi);

}

From source file:com.bangalore.barcamp.BCBUtils.java

public static void setAlarmForSession(Context context, Slot slot, Session session, int slotpos,
        int sessionpos) {
    BCBSharedPrefUtils.setAlarmSettingsForID(context, session.id, BCBSharedPrefUtils.ALARM_SET);
    PendingIntent intent = BCBUtils.createPendingIntentForID(context, session.id, slotpos, sessionpos);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    int hour = slot.startTime / 100;
    int mins = slot.startTime % 100;
    Log.e("Session", "hour : " + hour + " mins :" + mins);
    GregorianCalendar date = new GregorianCalendar(2013, Calendar.SEPTEMBER, 14, hour, mins);
    long timeInMills = date.getTimeInMillis() - 300000;
    alarmManager.set(AlarmManager.RTC_WAKEUP, timeInMills, intent);
}

From source file:com.battlelancer.seriesguide.util.Utils.java

/**
 * Run the notification service delayed by a minute to display and (re)schedule upcoming episode
 * alarms.//from   w ww  .  ja  v a 2  s  .com
 */
public static void runNotificationServiceDelayed(Context context) {
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(context, OnAlarmReceiver.class);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
    am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 1 * DateUtils.MINUTE_IN_MILLIS, pi);
}

From source file:com.commonsware.android.deepbg.PollReceiver.java

static void scheduleExactAlarm(Context ctxt, AlarmManager alarms, long period, boolean isDownload) {
    Intent i = buildBaseIntent(ctxt).putExtra(EXTRA_PERIOD, period).putExtra(EXTRA_IS_DOWNLOAD, isDownload);
    PendingIntent pi = PendingIntent.getBroadcast(ctxt, 0, i, 0);

    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
        Log.e("PollReceiver", "allow while idle");
        alarms.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                SystemClock.elapsedRealtime() + period, pi);
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        alarms.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + period, pi);
    } else {/* w  ww .  j a va 2s. com*/
        alarms.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + period, pi);
    }
}

From source file:org.chromium.chrome.browser.ntp.ContentSuggestionsNotificationHelper.java

@CalledByNative
private static boolean showNotification(int category, String idWithinCategory, String url, String title,
        String text, Bitmap image, long timeoutAtMillis) {
    if (findActiveNotification(category, idWithinCategory) != null)
        return false;

    // Post notification.
    Context context = ContextUtils.getApplicationContext();
    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    int nextId = nextNotificationId();
    Uri uri = Uri.parse(url);//from  w ww  . ja  va2s  .  co  m
    Intent contentIntent = new Intent(context, OpenUrlReceiver.class).setData(uri)
            .putExtra(NOTIFICATION_CATEGORY_EXTRA, category)
            .putExtra(NOTIFICATION_ID_WITHIN_CATEGORY_EXTRA, idWithinCategory);
    Intent deleteIntent = new Intent(context, DeleteReceiver.class).setData(uri)
            .putExtra(NOTIFICATION_CATEGORY_EXTRA, category)
            .putExtra(NOTIFICATION_ID_WITHIN_CATEGORY_EXTRA, idWithinCategory);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context).setAutoCancel(true)
            .setContentIntent(PendingIntent.getBroadcast(context, 0, contentIntent, 0))
            .setDeleteIntent(PendingIntent.getBroadcast(context, 0, deleteIntent, 0)).setContentTitle(title)
            .setContentText(text).setGroup(NOTIFICATION_TAG).setDefaults(NotificationCompat.DEFAULT_LIGHTS)
            .setPriority(-1).setLargeIcon(image).setSmallIcon(R.drawable.ic_chrome);
    manager.notify(NOTIFICATION_TAG, nextId, builder.build());
    addActiveNotification(new ActiveNotification(nextId, category, idWithinCategory, uri));

    // Set timeout.
    if (timeoutAtMillis != Long.MAX_VALUE) {
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent timeoutIntent = new Intent(context, TimeoutReceiver.class).setData(Uri.parse(url))
                .putExtra(NOTIFICATION_ID_EXTRA, nextId).putExtra(NOTIFICATION_CATEGORY_EXTRA, category)
                .putExtra(NOTIFICATION_ID_WITHIN_CATEGORY_EXTRA, idWithinCategory);
        alarmManager.set(AlarmManager.RTC, timeoutAtMillis,
                PendingIntent.getBroadcast(context, 0, timeoutIntent, PendingIntent.FLAG_UPDATE_CURRENT));
    }
    return true;
}

From source file:org.videolan.vlc.gui.dialogs.AdvOptionsDialog.java

public static void setSleep(Calendar time) {
    AlarmManager alarmMgr = (AlarmManager) VLCApplication.getAppContext()
            .getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(VLCApplication.SLEEP_INTENT);
    PendingIntent sleepPendingIntent = PendingIntent.getBroadcast(VLCApplication.getAppContext(), 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    if (time != null) {
        alarmMgr.set(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), sleepPendingIntent);
    } else {//www .j av  a  2 s  .  c o  m
        alarmMgr.cancel(sleepPendingIntent);
    }
    VLCApplication.sPlayerSleepTime = time;
}

From source file:org.microg.gms.gcm.McsService.java

public static void scheduleReconnect(Context context) {
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
    long delay = getCurrentDelay();
    logd("Scheduling reconnect in " + delay / 1000 + " seconds...");
    alarmManager.set(ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + delay, PendingIntent
            .getBroadcast(context, 1, new Intent(ACTION_RECONNECT, null, context, TriggerReceiver.class), 0));
}

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/* www.  j ava 2  s.c om*/
 */
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);
    }
}