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:net.vexelon.currencybg.app.ui.activities.MainActivity.java

/**
 * Starts background currencies update service
 *///  w  ww . ja  va 2  s  .c om
public void startService() {
    Intent myIntent = new Intent(MainActivity.this, RateService.class);
    pendingIntent = PendingIntent.getService(MainActivity.this, 0, myIntent, 0);
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.add(Calendar.SECOND, 30);
    long initialStartTimeout = calendar.getTimeInMillis();
    alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, initialStartTimeout, Defs.NOTIFY_INTERVAL,
            pendingIntent);
}

From source file:at.diamonddogs.util.CacheManager.java

/**
 * Turn on scheduled cache cleaning (cache will be cleaned even if app is
 * not running)//from   ww w .ja v a 2 s.c  o  m
 *
 * @param context a {@link Context}
 */
public void enableScheduledCacheCleaner(Context context) {
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    // @formatter:off
    am.setInexactRepeating(AlarmManager.RTC, Utils.getScheduledDate(Calendar.SUNDAY, 3, 0, 0).getTimeInMillis(),
            7 * AlarmManager.INTERVAL_DAY, getAlarmIntent(context));
    // @formatter:on
    Log.i(TAG, "Cache cleaning alarm has been set.");
}

From source file:ca.etsmtl.applets.etsmobile.NewsListActivity.java

private void setAlarm() {
    final Intent toAlarm = new Intent(this, NewsAlarmReceiver.class);
    final PendingIntent toDownload = PendingIntent.getBroadcast(this, 0, toAlarm,
            PendingIntent.FLAG_CANCEL_CURRENT);
    final AlarmManager alarms = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    final Calendar updateTime = Calendar.getInstance();
    updateTime.setTimeZone(TimeZone.getTimeZone("GMT"));

    updateTime.set(Calendar.HOUR_OF_DAY, 6);
    updateTime.set(Calendar.MINUTE, 00);
    alarms.setInexactRepeating(AlarmManager.RTC_WAKEUP, updateTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
            toDownload);/* w w  w.j  a v a  2  s . c  om*/

    updateTime.set(Calendar.HOUR_OF_DAY, 12);
    alarms.setInexactRepeating(AlarmManager.RTC_WAKEUP, updateTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
            toDownload);

    updateTime.set(Calendar.HOUR_OF_DAY, 18);
    alarms.setInexactRepeating(AlarmManager.RTC_WAKEUP, updateTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
            toDownload);
}

From source file:ca.zadrox.dota2esportticker.service.UpdateMatchService.java

private void scheduleAutoUpdates() {

    if (!PrefUtils.shouldAutoUpdate(this)) {
        return;/*from  w  w  w .j  a  va  2s .c  o  m*/
    }

    final long currentTime = TimeUtils.getUTCTime();

    final Intent updateMatchIntent = new Intent(UpdateMatchService.ACTION_AUTO_UPDATE_MATCHES, null, this,
            UpdateMatchService.class);

    final Intent updateResultIntent = new Intent(UpdateMatchService.ACTION_UPDATE_RESULTS, null, this,
            UpdateMatchService.class);

    final long matchUpdateTime = currentTime + (60000 * 60 * 12);
    final long resultUpdateTime = currentTime + (60000 * 60);

    PendingIntent umi = PendingIntent.getService(this.getApplicationContext(), 0, updateMatchIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    PendingIntent uri = PendingIntent.getService(this.getApplicationContext(), 0, updateResultIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    final AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);

    am.cancel(umi);
    am.cancel(uri);

    am.setInexactRepeating(AlarmManager.RTC, matchUpdateTime, AlarmManager.INTERVAL_HALF_DAY, umi);

    am.setInexactRepeating(AlarmManager.RTC, resultUpdateTime, AlarmManager.INTERVAL_HOUR, uri);
}

From source file:net.vexelon.bgrates.ui.activities.MainActivity.java

public void startService() {
    Intent myIntent = new Intent(MainActivity.this, RateService.class);
    pendingIntent = PendingIntent.getService(MainActivity.this, 0, myIntent, 0);
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.add(Calendar.SECOND, 30);
    long initialStartTimeout = calendar.getTimeInMillis();
    alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, initialStartTimeout, Defs.NOTIFY_INTERVAL,
            pendingIntent);/*  w  ww .  j a  va2s . c om*/
}

From source file:org.svij.taskwarriorapp.TasksActivity.java

@Override
protected void onResume() {
    super.onResume();

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    long date_long = prefs.getLong("notifications_alarm_time", System.currentTimeMillis());

    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
    Intent i = new Intent(this, NotificationService.class);
    PendingIntent pi = PendingIntent.getService(this, 0, i, 0);

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(date_long);

    if (!calendar.getTime().before(new Date())) {
        am.cancel(pi);//w  ww  .  ja v a  2  s  .c o m
        am.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                calendar.getTimeInMillis() + AlarmManager.INTERVAL_DAY, pi);
    }

}

From source file:com.achow101.bitcointalkforum.MainActivity.java

public void onResume() {
    super.onResume();
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    int seconds = Integer.parseInt(prefs.getString("notifications_sync_freq", "60"));
    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
    Intent i = new Intent(this, NotificationService.class);
    i.putExtra("SESSION ID", sessId);
    i.putExtra("Username", mUsername);
    PendingIntent pi = PendingIntent.getService(this, 0, i, 0);
    am.cancel(pi);/*from   www.j a  v  a2 s  . c om*/

    if (prefs.getBoolean("notifications_new_message", true)) {
        am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                SystemClock.elapsedRealtime() + seconds * 1000, seconds * 1000, pi);
    }
}

From source file:org.ametro.app.ApplicationEx.java

public void changeAlarmReceiverState(boolean enabled) {
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    Intent intent = new Intent(this, AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
    alarmManager.cancel(pendingIntent);//from  ww  w .  jav a  2 s .  c  o m
    if (enabled) {
        long interval = (GlobalSettings.getUpdatePeriod(this) == 900) ? AlarmManager.INTERVAL_FIFTEEN_MINUTES
                : AlarmManager.INTERVAL_DAY;
        alarmManager.setInexactRepeating(AlarmManager.RTC, System.currentTimeMillis() + 1000 * 60 * 2, interval,
                pendingIntent);
    }
}

From source file:io.demiseq.jetreader.activities.MainActivity.java

private void setAlarmService() {
    if (!prefs.getBoolean("Alarm", false)) {
        Intent i = new Intent(this, FetchLatestService.class);
        PendingIntent pi = PendingIntent.getService(this, 0, i, 0);
        AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
        am.cancel(pi);//  w  w  w.  j av  a2  s  .c o m
        String frequency = prefs.getString(GeneralSettingsActivity.KEY_UPDATE_FREQUENCY, "1440");
        int minute = Integer.parseInt(frequency);
        am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                SystemClock.elapsedRealtime() + minute * 60 * 1000, minute * 60 * 1000, pi);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putBoolean("Alarm", true);
        editor.apply();
    }
}

From source file:com.money.manager.ex.sync.SyncSchedulerBroadcastReceiver.java

private void startHeartbeat(Context context, AlarmManager alarmManager, PendingIntent pendingIntent) {
    SyncManager sync = new SyncManager(context);
    if (!sync.isSyncEnabled())
        return;//from www  .java 2 s.c om

    // get frequency in minutes.
    SyncPreferences preferences = new SyncPreferences(context);
    int minutes = preferences.getSyncInterval();
    // If the period is 0, do not schedule the alarm.
    if (minutes <= 0)
        return;

    MmxDate now = new MmxDate();
    int secondsInMinute = 60;

    Timber.d("Scheduling synchronisation at: %s, repeat every %s minutes", now.toString(), minutes);

    // Schedule the alarm for synchronization. Run immediately and then in the given interval.
    alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, now.getMillis(), minutes * secondsInMinute * 1000,
            pendingIntent);
}