Example usage for android.app AlarmManager ELAPSED_REALTIME_WAKEUP

List of usage examples for android.app AlarmManager ELAPSED_REALTIME_WAKEUP

Introduction

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

Prototype

int ELAPSED_REALTIME_WAKEUP

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

Click Source Link

Document

Alarm time in android.os.SystemClock#elapsedRealtime SystemClock.elapsedRealtime() (time since boot, including sleep), which will wake up the device when it goes off.

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);
    /*//ww w  .jav a2  s  . c  o  m
     * 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.
 *//*  w  w w . j  a v  a2  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:com.andrewshu.android.reddit.mail.EnvelopeService.java

public static void resetAlarm(Context context, long interval) {
    // Create an IntentSender that will launch our service, to be scheduled
    // with the alarm manager.
    PendingIntent alarmSender = PendingIntent.getService(context, 0, new Intent(context, EnvelopeService.class),
            0);//from w w  w .  ja  v a 2  s  .co  m
    AlarmManager am = (AlarmManager) context.getSystemService(ALARM_SERVICE);
    am.cancel(alarmSender);
    if (interval != 0)
        am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), interval,
                alarmSender);
}

From source file:me.oss.tracker.trackme.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    this.context = this;

    textView = (TextView) findViewById(R.id.deviceID);

    /* Handset mac*/
    TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    String device_id = tm.getDeviceId();
    textView.setText(device_id + "");

    Intent alarm = new Intent(this.context, AlarmReceiver.class);
    boolean alarmRunning = (PendingIntent.getBroadcast(this.context, 0, alarm,
            PendingIntent.FLAG_NO_CREATE) != null);
    if (alarmRunning == false) {
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this.context, 0, alarm, 0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(),
                LocalLog.syn_time, pendingIntent);
    }/*from w w w  . j  a va 2 s. c o  m*/

    mRegistrationProgressBar = (ProgressBar) findViewById(R.id.registrationProgressBar);
    mRegistrationBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            mRegistrationProgressBar.setVisibility(ProgressBar.GONE);
            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
            boolean sentToken = sharedPreferences.getBoolean(QuickstartPreferences.SENT_TOKEN_TO_SERVER, false);
            if (sentToken) {
                mInformationTextView.setText(getString(R.string.gcm_send_message));
            } else {
                mInformationTextView.setText(getString(R.string.token_error_message));
            }
        }
    };
    mInformationTextView = (TextView) findViewById(R.id.informationTextView);

    if (checkPlayServices()) {
        // Start IntentService to register this application with GCM.
        Intent intent = new Intent(this, RegistrationIntentService.class);
        startService(intent);
    }
}

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 av a2s . 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:cc.softwarefactory.lokki.android.services.DataService.java

private void setTimer() {

    alarm = (AlarmManager) getSystemService(ALARM_SERVICE);
    Intent alarmIntent = new Intent(this, DataService.class);
    alarmIntent.putExtra(ALARM_TIMER, 1);
    alarmCallback = PendingIntent.getService(this, 0, alarmIntent, 0);
    alarm.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 0, 30 * 1000, alarmCallback);
    Log.e(TAG, "Timer created.");
}

From source file:alaindc.memenguage.RandomIntentService.java

private void setTimeout() {
    // Set the alarms for next sensing of amplitude
    alarmMgr = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);

    Intent intentAlarm = new Intent(getApplicationContext(), RandomIntentService.class);
    intentAlarm.setAction(Constants.ACTION_RANDOM_WORD);
    alarmIntent = PendingIntent.getService(getApplicationContext(), 0, intentAlarm, 0);

    try {/*from ww w  .  jav a 2  s.  com*/
        // Remove the oldest one if exists
        alarmMgr.cancel(alarmIntent);
    } catch (Exception e) {
        Log.d("Randomintentservice", "Cancel pending intent error");
    }

    long millisec = Long.parseLong(
            PreferenceManager.getDefaultSharedPreferences(this).getString("interval_notifications", "120")) * 60
            * 1000;
    alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + millisec, alarmIntent);
}

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

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

    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:ca.spencerelliott.mercury.ChangesetService.java

@Override
public void onStart(Intent intent, int startId) {
    int broadcast_call = intent.getIntExtra("ca.spencerelliott.mercury.call", -1);

    //If this was on boot we want to set up the alarm to set up repeating notifications
    if (broadcast_call == AlarmReceiver.ON_BOOT) {
        //Get the alarm service
        AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

        //Generate the interval between notifications, default to fifteen minutes
        long interval = Long.parseLong(prefs.getString("notification_interval", "900000"));

        //Create the intents to launch the service again
        Intent new_intent = new Intent("ca.spencerelliott.mercury.REFRESH_CHANGESETS");
        PendingIntent p_intent = PendingIntent.getBroadcast(this, 0, new_intent, 0);

        final Calendar c = Calendar.getInstance();

        //Create a repeating alarm
        alarm.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, c.getTimeInMillis() + interval,
                interval, p_intent);/*from   www . j  av a 2 s  . co m*/

        //Stop the service since we're waiting for the interval
        stopSelf();
    }

    //Create a new feed processor
    FeedProcessor processor = new FeedProcessor();

    //Gather all of the URLs from the databases here

    //Let the processor handle all of the URLs and notify the user of any new changesets
    processor.execute();

}

From source file:cc.softwarefactory.lokki.android.services.LocationService.java

private void setTemporalTimer() {
    AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE);
    Intent alarmIntent = new Intent(this, LocationService.class);
    alarmIntent.putExtra(ALARM_TIMER, 1);
    PendingIntent alarmCallback = PendingIntent.getService(this, 0, alarmIntent, 0);
    alarm.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + INTERVAL_1_MIN,
            alarmCallback);//from   w  w  w . j  av  a2s  .c  om
    Log.e(TAG, "Time created.");
}