Example usage for android.app AlarmManager setInexactRepeating

List of usage examples for android.app AlarmManager setInexactRepeating

Introduction

In this page you can find the example usage for android.app AlarmManager setInexactRepeating.

Prototype

public void setInexactRepeating(@AlarmType int type, long triggerAtMillis, long intervalMillis,
        PendingIntent operation) 

Source Link

Document

Schedule a repeating alarm that has inexact trigger time requirements; for example, an alarm that repeats every hour, but not necessarily at the top of every hour.

Usage

From source file:com.oliversride.wordryo.UpdateCheckReceiver.java

public static void restartTimer(Context context) {
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    Intent intent = new Intent(context, UpdateCheckReceiver.class);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
    am.cancel(pi);//from  w w w .  j  a v  a  2s .c  o  m

    long interval_millis = INTERVAL_ONEDAY;
    if (!devOK(context)) {
        interval_millis *= INTERVAL_NDAYS;
    }
    interval_millis = (interval_millis / 2) + Math.abs(Utils.nextRandomInt() % interval_millis);
    am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
            SystemClock.elapsedRealtime() + interval_millis, interval_millis, pi);
}

From source file:mchs.neverforget.services.NotificationIntentService.java

public static void startCustomRecurrentNotificationAlarm(Context context, int customId) {
    //Toast.makeText(context, "startCustomRecurrentNotificationAlarm"+customId, Toast.LENGTH_SHORT).show();
    Intent intent = new Intent(context, NotificationIntentService.class);
    if (customId == 1) {
        intent.setAction(ACTION_NOTIFY_CUSTOM1);
    } else if (customId == 2) {
        intent.setAction(ACTION_NOTIFY_CUSTOM2);
    } else if (customId == 3) {
        intent.setAction(ACTION_NOTIFY_CUSTOM3);
    }//from w  w  w. j ava 2s .  c o m

    PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent,
            PendingIntent.FLAG_CANCEL_CURRENT);

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

    String[] hourOfDay = PreferenceManager.getDefaultSharedPreferences(context)
            .getString("notification_time_picker" + customId, "12:00").split(":");
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hourOfDay[0]));
    calendar.set(Calendar.MINUTE, Integer.parseInt(hourOfDay[1]));

    if (calendar.getTimeInMillis() < System.currentTimeMillis()) {
        calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + 1);
    }
    alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
            AlarmManager.INTERVAL_DAY, pendingIntent);

}

From source file:net.kourlas.voipms_sms.receivers.SynchronizationIntervalReceiver.java

public static void setupSynchronizationInterval(Context applicationContext) {
    Preferences preferences = Preferences.getInstance(applicationContext);
    AlarmManager alarmManager = (AlarmManager) applicationContext.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(applicationContext, SynchronizationIntervalReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(applicationContext, 0, intent, 0);

    alarmManager.cancel(pendingIntent);/*from w w  w .  j  ava2  s .  com*/

    long syncInterval = preferences.getSyncInterval() * (24 * 60 * 60 * 1000);
    if (syncInterval != 0) {
        long nextSyncTime = preferences.getLastCompleteSyncTime() + syncInterval;

        long now = System.currentTimeMillis();
        if (nextSyncTime <= now) {
            Database.getInstance(applicationContext).synchronize(false, false, null);
            nextSyncTime = now + syncInterval;
        }

        alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, nextSyncTime, syncInterval, pendingIntent);
    }
}

From source file:com.google.android.apps.dashclock.PeriodicExtensionRefreshReceiver.java

/**
 * Sets the refresh schedule if one isn't set already.
 *///from w  w w  .  ja v a 2 s.  c o  m
public static void updateExtensionsAndEnsurePeriodicRefresh(final Context context) {
    LOGD(TAG, "updateExtensionsAndEnsurePeriodicRefresh");
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    // Update all extensions now.
    context.startService(getUpdateAllExtensionsIntent(context, DashClockExtension.UPDATE_REASON_MANUAL));

    // Schedule an alarm for every 30 minutes; it will not wake up the device;
    // it will be handled only once the device is awake. The first time that this
    // alarm can go off is in 15 minutes, and the latest time it will go off is
    // 45 minutes from now.
    PendingIntent pi = PendingIntent.getBroadcast(context, 0,
            new Intent(context, PeriodicExtensionRefreshReceiver.class).setAction(ACTION_PERIODIC_ALARM),
            PendingIntent.FLAG_UPDATE_CURRENT);
    am.cancel(pi);
    am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
            SystemClock.elapsedRealtime() + 15 * MINUTES_MILLIS, AlarmManager.INTERVAL_HALF_HOUR, pi);
}

From source file:Main.java

public static void startAlert(AlarmManager alarmMgr, PendingIntent alarmIntent, Context context) {

    //        alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    //        Intent intent = new Intent(context, AlerReceiver.class);
    //        alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

    // Set the alarm to start at 8:30 a.m.
    //        Calendar calendar = Calendar.getInstance();
    //        calendar.setTimeInMillis(System.currentTimeMillis());
    //        calendar.set(Calendar.HOUR_OF_DAY, 8);
    //        calendar.set(Calendar.MINUTE, 30);

    // setRepeating() lets you specify a precise custom interval--in this case,
    // 20 minutes.
    //        alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
    //                1000 * 60 * 20, alarmIntent);

    alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 3000, alarmIntent);

}

From source file:id.ridon.keude.UpdateService.java

public static void schedule(Context ctx) {

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
    String sint = prefs.getString(Preferences.PREF_UPD_INTERVAL, "0");
    int interval = Integer.parseInt(sint);

    Intent intent = new Intent(ctx, UpdateService.class);
    PendingIntent pending = PendingIntent.getService(ctx, 0, intent, 0);

    AlarmManager alarm = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
    alarm.cancel(pending);//from   www . ja  va  2  s  .c  om
    if (interval > 0) {
        alarm.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 5000,
                AlarmManager.INTERVAL_HOUR, pending);
        Log.d("Keude", "Update scheduler alarm set");
    } else {
        Log.d("Keude", "Update scheduler alarm not set");
    }

}

From source file:de.schildbach.wallet.WalletApplication.java

public static void scheduleStartBlockchainService(final Context context) {
    final Configuration config = new Configuration(PreferenceManager.getDefaultSharedPreferences(context));
    final long lastUsedAgo = config.getLastUsedAgo();

    // apply some backoff
    final long alarmInterval;
    if (lastUsedAgo < Constants.LAST_USAGE_THRESHOLD_JUST_MS)
        alarmInterval = AlarmManager.INTERVAL_FIFTEEN_MINUTES;
    else if (lastUsedAgo < Constants.LAST_USAGE_THRESHOLD_RECENTLY_MS)
        alarmInterval = AlarmManager.INTERVAL_HALF_DAY;
    else//from www  . j a  v  a 2  s .  c o  m
        alarmInterval = AlarmManager.INTERVAL_DAY;

    log.info("last used {} minutes ago, rescheduling blockchain sync in roughly {} minutes",
            lastUsedAgo / DateUtils.MINUTE_IN_MILLIS, alarmInterval / DateUtils.MINUTE_IN_MILLIS);

    final AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    final PendingIntent alarmIntent = PendingIntent.getService(context, 0,
            new Intent(context, BlockchainServiceImpl.class), 0);
    alarmManager.cancel(alarmIntent);

    // workaround for no inexact set() before KitKat
    final long now = System.currentTimeMillis();
    alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, now + alarmInterval, AlarmManager.INTERVAL_DAY,
            alarmIntent);
}

From source file:biz.wiz.android.wallet.WalletApplication.java

public static void scheduleStartBlockchainService(@Nonnull final Context context) {
    final Configuration config = new Configuration(PreferenceManager.getDefaultSharedPreferences(context));
    final long lastUsedAgo = config.getLastUsedAgo();

    // apply some backoff
    final long alarmInterval;
    if (lastUsedAgo < Constants.LAST_USAGE_THRESHOLD_JUST_MS)
        alarmInterval = AlarmManager.INTERVAL_FIFTEEN_MINUTES;
    else if (lastUsedAgo < Constants.LAST_USAGE_THRESHOLD_RECENTLY_MS)
        alarmInterval = AlarmManager.INTERVAL_HALF_DAY;
    else//w  w  w .  j  ava  2s  .  co  m
        alarmInterval = AlarmManager.INTERVAL_DAY;

    log.info("last used {} minutes ago, rescheduling blockchain sync in roughly {} minutes",
            lastUsedAgo / DateUtils.MINUTE_IN_MILLIS, alarmInterval / DateUtils.MINUTE_IN_MILLIS);

    final AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    final PendingIntent alarmIntent = PendingIntent.getService(context, 0,
            new Intent(context, BlockchainServiceImpl.class), 0);
    alarmManager.cancel(alarmIntent);

    // workaround for no inexact set() before KitKat
    final long now = System.currentTimeMillis();
    alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, now + alarmInterval, AlarmManager.INTERVAL_DAY,
            alarmIntent);
}

From source file:org.hopestarter.wallet.WalletApplication.java

public static void scheduleStartBlockchainService(final Context context) {
    final Configuration config = new Configuration(PreferenceManager.getDefaultSharedPreferences(context),
            context.getResources());/*from  w ww .  j  a  v  a2 s.co  m*/
    final long lastUsedAgo = config.getLastUsedAgo();

    // apply some backoff
    final long alarmInterval;
    if (lastUsedAgo < Constants.LAST_USAGE_THRESHOLD_JUST_MS)
        alarmInterval = AlarmManager.INTERVAL_FIFTEEN_MINUTES;
    else if (lastUsedAgo < Constants.LAST_USAGE_THRESHOLD_RECENTLY_MS)
        alarmInterval = AlarmManager.INTERVAL_HALF_DAY;
    else
        alarmInterval = AlarmManager.INTERVAL_DAY;

    log.info("last used {} minutes ago, rescheduling blockchain sync in roughly {} minutes",
            lastUsedAgo / DateUtils.MINUTE_IN_MILLIS, alarmInterval / DateUtils.MINUTE_IN_MILLIS);

    final AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    final PendingIntent alarmIntent = PendingIntent.getService(context, 0,
            new Intent(context, BlockchainServiceImpl.class), 0);
    alarmManager.cancel(alarmIntent);

    // workaround for no inexact set() before KitKat
    final long now = System.currentTimeMillis();
    alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, now + alarmInterval, AlarmManager.INTERVAL_DAY,
            alarmIntent);
}

From source file:de.langerhans.wallet.WalletApplication.java

public static void scheduleStartBlockchainService(@Nonnull final Context context) {
    final Configuration config = new Configuration(PreferenceManager.getDefaultSharedPreferences(context));
    final long lastUsedAgo = config.getLastUsedAgo();

    // apply some backoff
    final long alarmInterval;
    if (lastUsedAgo < Constants.LAST_USAGE_THRESHOLD_JUST_MS)
        alarmInterval = AlarmManager.INTERVAL_FIFTEEN_MINUTES;
    else if (lastUsedAgo < Constants.LAST_USAGE_THRESHOLD_RECENTLY_MS)
        alarmInterval = AlarmManager.INTERVAL_HALF_DAY;
    else//from  w  w w .j a v  a 2  s .co  m
        alarmInterval = AlarmManager.INTERVAL_DAY;

    log.info("last used {} minutes ago, rescheduling blockchain sync in roughly {} minutes",
            lastUsedAgo / DateUtils.MINUTE_IN_MILLIS, alarmInterval / DateUtils.MINUTE_IN_MILLIS);

    final AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    final Intent startIntent = new Intent(context, AutosyncReceiver.class);
    startIntent.setAction("de.langerhans.wallet.AUTOSYNC_ACTION");
    final PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, startIntent, 0);
    alarmManager.cancel(alarmIntent);

    // workaround for no inexact set() before KitKat
    final long now = System.currentTimeMillis();
    alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, now + alarmInterval, AlarmManager.INTERVAL_DAY,
            alarmIntent);
}