Example usage for android.app AlarmManager INTERVAL_HALF_HOUR

List of usage examples for android.app AlarmManager INTERVAL_HALF_HOUR

Introduction

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

Prototype

long INTERVAL_HALF_HOUR

To view the source code for android.app AlarmManager INTERVAL_HALF_HOUR.

Click Source Link

Document

Available inexact recurrence interval recognized by #setInexactRepeating(int,long,long,PendingIntent) when running on Android prior to API 19.

Usage

From source file:com.licenta.android.licenseapp.alarm.AlarmReceiver.java

public static void setAlarm(Context context) {
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, AlarmReceiver.class);
    PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    /*/*from  w  w  w  . ja  v  a2 s  .c  om*/
     * If you don't have precise time requirements, use an inexact repeating alarm
     * the minimize the drain on the device battery.
     *
     * The call below specifies the alarm type, the trigger time, the interval at
     * which the alarm is fired, and the alarm's associated PendingIntent.
     * It uses the alarm type RTC_WAKEUP ("Real Time Clock" wake up), which wakes up
     * the device and triggers the alarm according to the time of the device's clock.
     *
     * Alternatively, you can use the alarm type ELAPSED_REALTIME_WAKEUP to trigger
     * an alarm based on how much time has elapsed since the device was booted. This
     * is the preferred choice if your alarm is based on elapsed time--for example, if
     * you simply want your alarm to fire every 60 minutes. You only need to use
     * RTC_WAKEUP if you want your alarm to fire at a particular date/time. Remember
     * that clock-based time may not translate well to other locales, and that your
     * app's behavior could be affected by the user changing the device's time setting.
     *
     */

    // Wake up the device to fire the alarm in 30 minutes, and every 30 minutes
    // after that.
    long intervalMillis;
    String intervalVal = prefs.getString("repeat_interval", "0");
    switch (intervalVal) {
    case "15":
        intervalMillis = AlarmManager.INTERVAL_FIFTEEN_MINUTES;
        break;
    case "30":
        intervalMillis = AlarmManager.INTERVAL_HALF_HOUR;
        break;
    case "60":
        intervalMillis = AlarmManager.INTERVAL_HOUR;
        break;
    default:
        intervalMillis = 0;
        break;
    }

    // for testing
    intervalMillis = 6000;

    alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
            SystemClock.elapsedRealtime() + intervalMillis, intervalMillis, alarmIntent);

    prefs.edit().putBoolean(Constants.PREF_KEY_IS_ALARM_ON, true).apply();
    Log.d(context.getClass().getName(), "Alarm started");

}

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 va 2s .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:edu.mit.media.realityanalysis.fieldtest.MainPipeline.java

@Override
public void onCreate() {
    super.onCreate();

    String password = getDataPassword(this);

    // If the user hasn't logged in yet, we need to stop the service 
    // NOTE: this currently doesn't work properly because we're just using the default password (changeme)
    if (password == null || password == "") {
        Intent loginIntent = new Intent(this, LoginActivity.class);
        startActivity(loginIntent);//from w  w w .j a va2 s  . com
        this.stopSelf();
    } else {
        setEncryptionPassword(getDataPassword(this).toCharArray());
    }

    //Setup the notification service to run periodically
    Intent notificationIntent = new Intent(this, NotificationService.class);

    PendingIntent notificationPendingIntent = PendingIntent.getService(this, 0, notificationIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

    alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
            AlarmManager.INTERVAL_HALF_HOUR, notificationPendingIntent);
}

From source file:com.twentyoneechoes.borges.util.Utils.java

public static void scheduleLibraryUpdateService(Context context) {
    if (isAlarmAlreadySet(context))
        return;//from w  w w.j av  a  2  s. co m

    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, LibraryUpdateService.class);
    PendingIntent alarmIntent = PendingIntent.getService(context, LIBRARY_UPDATE_REQUEST_CODE, intent, 0);

    alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, AlarmManager.INTERVAL_HALF_HOUR,
            AlarmManager.INTERVAL_HALF_HOUR, alarmIntent);
}

From source file:com.jerrellmardis.amphitheatre.util.Utils.java

public static void scheduleLibraryUpdateService(Context context) {
    Log.d("amp:Utils",
            "Checking if alarm is already set: " + isAlarmAlreadySet(context, LibraryUpdateService.class));
    //And now//  w w w  .j  a va 2 s  .  co m
    Log.d("amp:Utils", "Start rechecking the library");
    Intent intent = new Intent(context, LibraryUpdateService.class);
    intent.putExtra("ALARM", true);
    /*context.startService(intent);*/
    if (isAlarmAlreadySet(context, LibraryUpdateService.class))
        return;

    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    //Intent intent = ...
    PendingIntent alarmIntent = PendingIntent.getService(context, LIBRARY_UPDATE_REQUEST_CODE, intent, 0);

    alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, AlarmManager.INTERVAL_HALF_HOUR,
            AlarmManager.INTERVAL_HOUR * 3, //Every three hours seems sufficient
            alarmIntent);

}

From source file:app.jorge.mobile.com.transportalert.ScrollingActivity.java

public void scheduleAlarm() {
    // Construct an intent that will execute the AlarmReceiver
    Intent intent = new Intent(getApplicationContext(), MyAlarmReceiver.class);
    // Create a PendingIntent to be triggered when the alarm goes off
    final PendingIntent pIntent = PendingIntent.getBroadcast(this, MyAlarmReceiver.REQUEST_CODE, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    // Setup periodic alarm every 5 seconds
    long firstMillis = System.currentTimeMillis(); // alarm is set right away
    AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
    // First parameter is the type: ELAPSED_REALTIME, ELAPSED_REALTIME_WAKEUP, RTC_WAKEUP
    // Interval can be INTERVAL_FIFTEEN_MINUTES, INTERVAL_HALF_HOUR, INTERVAL_HOUR, INTERVAL_DAY

    //alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstMillis,
    //      AlarmManager.INTERVAL_HALF_HOUR, pIntent);

    alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstMillis, AlarmManager.INTERVAL_HALF_HOUR, pIntent);
}

From source file:com.jerrellmardis.amphitheatre.util.Utils.java

public static void scheduleRecommendations(Context context) {
    Log.d("amp:Utils",
            "Checking if alarm is already set: " + isAlarmAlreadySet(context, RecommendationsService.class));
    //And now/*from  w  ww .  j a v a 2s  . c om*/
    Log.d("amp:Utils", "Start rechecking the recommendations");
    Intent intent = new Intent(context, RecommendationsService.class);
    intent.putExtra("ALARM", true);
    /*context.startService(intent);*/
    if (isAlarmAlreadySet(context, RecommendationsService.class))
        return;

    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    //Intent intent = ...
    PendingIntent alarmIntent = PendingIntent.getService(context, LIBRARY_UPDATE_REQUEST_CODE, intent, 0);

    alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 5000,
            AlarmManager.INTERVAL_HALF_HOUR, //Every half hour seems sufficient
            alarmIntent);

}

From source file:com.near.chimerarevo.fragments.SettingsFragment.java

private void setAlarm(int sel, boolean isEnabled) {
    Intent intent = new Intent(getActivity().getApplicationContext(), NewsService.class);
    PendingIntent pintent = PendingIntent.getService(getActivity().getApplicationContext(), 0, intent, 0);
    AlarmManager alarm = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);

    alarm.cancel(pintent);//from   w w  w.j a  va  2  s.  co  m

    if (isEnabled) {
        long delay;
        switch (sel) {
        case 0:
            delay = AlarmManager.INTERVAL_FIFTEEN_MINUTES;
            break;
        case 1:
            delay = AlarmManager.INTERVAL_HALF_HOUR;
            break;
        case 2:
            delay = AlarmManager.INTERVAL_HOUR;
            break;
        case 3:
            delay = 2 * AlarmManager.INTERVAL_HOUR;
            break;
        case 4:
            delay = 3 * AlarmManager.INTERVAL_HOUR;
            break;
        case 5:
            delay = 6 * AlarmManager.INTERVAL_HOUR;
            break;
        case 6:
            delay = AlarmManager.INTERVAL_HALF_DAY;
            break;
        case 7:
            delay = AlarmManager.INTERVAL_DAY;
            break;
        default:
            delay = AlarmManager.INTERVAL_HOUR;
            break;
        }

        alarm.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.uptimeMillis(), delay, pintent);
    }
}

From source file:de.hero.vertretungsplan.MainActivity.java

public static void setNewAlarm(Context context, boolean set, String interval) {
    Intent i = new Intent(context, de.hero.vertretungsplan.CheckForUpdates.class);
    PendingIntent pi = PendingIntent.getService(context, 0, i, 0);
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    am.cancel(pi); // cancel any existing alarms
    Log.d("MainActivity", "cancelAlarm");
    if (set) {/*from w  w  w . j  av  a2  s .  c om*/

        long lngInterval;
        switch (interval) {
        case "1/2":
            lngInterval = AlarmManager.INTERVAL_HALF_HOUR;
            break;
        case "1":
            lngInterval = AlarmManager.INTERVAL_HOUR;
            break;
        case "6":
            lngInterval = AlarmManager.INTERVAL_HALF_DAY / 2;
            break;
        case "3":
        default:
            lngInterval = AlarmManager.INTERVAL_HOUR * 3;
            break;
        }
        Log.d("MainActivity", "setAlarm " + interval + " Stunden");
        am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME,
                SystemClock.elapsedRealtime() + AlarmManager.INTERVAL_HALF_HOUR / 3, lngInterval, pi);
        // For debugging after 10 seconds
        // am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
        // SystemClock.elapsedRealtime() + 25000 ,
        // AlarmManager.INTERVAL_HOUR , pi);

    }
}

From source file:com.near.chimerarevo.activities.MainActivity.java

@Override
public void onDestroy() {
    super.onDestroy();
    if (ImageLoader.getInstance().isInited()) {
        ImageLoader.getInstance().clearDiskCache();
        ImageLoader.getInstance().clearMemoryCache();
    }// ww  w  .  j  av  a  2 s.c  o m
    if (mHelper != null)
        mHelper.dispose();

    if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean("news_search_pref", true)) {
        Intent intent = new Intent(getApplicationContext(), NewsService.class);
        PendingIntent pintent = PendingIntent.getService(getApplicationContext(), 0, intent, 0);
        AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        alarm.cancel(pintent);

        long delay;
        int sel = Integer.parseInt(
                PreferenceManager.getDefaultSharedPreferences(this).getString("notification_delay_pref", "0"));

        switch (sel) {
        case 0:
            delay = AlarmManager.INTERVAL_FIFTEEN_MINUTES;
            break;
        case 1:
            delay = AlarmManager.INTERVAL_HALF_HOUR;
            break;
        case 2:
            delay = AlarmManager.INTERVAL_HOUR;
            break;
        case 3:
            delay = 2 * AlarmManager.INTERVAL_HOUR;
            break;
        case 4:
            delay = 3 * AlarmManager.INTERVAL_HOUR;
            break;
        case 5:
            delay = 6 * AlarmManager.INTERVAL_HOUR;
            break;
        case 6:
            delay = AlarmManager.INTERVAL_HALF_DAY;
            break;
        case 7:
            delay = AlarmManager.INTERVAL_DAY;
            break;
        default:
            delay = AlarmManager.INTERVAL_HOUR;
            break;
        }

        alarm.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.uptimeMillis(), delay, pintent);
    }
}