Example usage for android.app AlarmManager ELAPSED_REALTIME

List of usage examples for android.app AlarmManager ELAPSED_REALTIME

Introduction

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

Prototype

int ELAPSED_REALTIME

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

Click Source Link

Document

Alarm time in android.os.SystemClock#elapsedRealtime SystemClock.elapsedRealtime() (time since boot, including sleep).

Usage

From source file:Main.java

public static void startServiceAlarm(Context context, Class<?> service, long interval) {
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(context, service);
    PendingIntent p = PendingIntent.getService(context, 10000, i, PendingIntent.FLAG_CANCEL_CURRENT);
    am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, 0, interval, p);
}

From source file:Main.java

public static boolean setAlarmByBC(Context context, Class<?> bc_class, int period) {
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    if (alarmManager == null)
        return false;
    long triggerAtTime = SystemClock.elapsedRealtime() + period;
    Intent i = new Intent(context, bc_class);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
    alarmManager.set(AlarmManager.ELAPSED_REALTIME, triggerAtTime, pi);
    return true;//from  w ww  . ja  va  2s .  c om
}

From source file:MainActivity.java

public void setAlarm(View view) {
    Intent intentToFire = new Intent(getApplicationContext(), AlarmBroadcastReceiver.class);
    intentToFire.setAction(AlarmBroadcastReceiver.ACTION_ALARM);
    PendingIntent alarmIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intentToFire, 0);
    AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    long thirtyMinutes = SystemClock.elapsedRealtime() + 30 * 1000;
    alarmManager.set(AlarmManager.ELAPSED_REALTIME, thirtyMinutes, alarmIntent);

    //alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME, thirtyMinutes, AlarmManager.INTERVAL_HALF_HOUR, alarmIntent);
}

From source file:com.yeldi.yeldibazaar.UpdateService.java

public static void schedule(Context ctx) {

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
    String sint = prefs.getString("updateInterval", "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);/*ww w  .j  a va  2s.c om*/
    if (interval > 0) {
        alarm.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 5000,
                AlarmManager.INTERVAL_HOUR, pending);
        Log.d("FDroid", "Update scheduler alarm set");
    } else {
        Log.d("FDroid", "Update scheduler alarm not set");
    }

}

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

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    boolean setTimer = intent.getBooleanExtra("setTimer", false);
    boolean checkNow = intent.getBooleanExtra("checkNow", false);

    Intent i = new Intent(this, de.hero.vertretungsplan.CheckForAppUpdate.class);
    i.putExtra("checkNow", true);
    PendingIntent pi = PendingIntent.getService(this, 0, i, 0);
    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    am.cancel(pi);/*from  w  ww .j ava 2  s  . c om*/
    Log.d("CheckForAppUpdate", "Alarm canceled");
    if (setTimer) {

        long lngInterval = AlarmManager.INTERVAL_DAY * 7;
        Log.d("CheckForAppUpdate", "Alarm set");
        am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME,
                SystemClock.elapsedRealtime() + AlarmManager.INTERVAL_DAY * 6, lngInterval, pi);
        //For debugging: after 10 seconds
        //am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 25000 , AlarmManager.INTERVAL_HOUR , pi);

    }

    if (checkNow) {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

        if (cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected()) {
            try {
                new HtmlWorkAppUpdate(this).execute(new URI(getString(R.string.htmlAdresseAktuelleVersion)));
            } catch (URISyntaxException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    return START_NOT_STICKY;
}

From source file:com.cyanogenmod.account.util.CMAccountUtils.java

public static void scheduleRetry(Context context, SharedPreferences prefs, Intent intent) {
    int backoffTimeMs = getBackoff(prefs);
    int nextAttempt = backoffTimeMs / 2 + sRandom.nextInt(backoffTimeMs);
    if (CMAccount.DEBUG)
        Log.d(TAG, "Scheduling retry, backoff = " + nextAttempt + " (" + backoffTimeMs + ") for "
                + intent.getAction());// ww w . j  av a2 s. c  o m
    PendingIntent retryPendingIntent = PendingIntent.getService(context, 0, intent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    am.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + nextAttempt, retryPendingIntent);
    if (backoffTimeMs < CMAccount.MAX_BACKOFF_MS) {
        setBackoff(prefs, backoffTimeMs * 2);
    }
}

From source file:ch.fixme.status.Widget.java

protected static void setAlarm(Context ctxt, Intent i, int widgetId, int delay) {
    // Get interval
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctxt);
    long update_interval = Long
            .parseLong(prefs.getString(Prefs.KEY_CHECK_INTERVAL, Prefs.DEFAULT_CHECK_INTERVAL)) * 60L * 1000L;
    // Set alarm/* w ww  .  j  a  v  a2s. c  om*/
    AlarmManager am = (AlarmManager) ctxt.getSystemService(Context.ALARM_SERVICE);
    PendingIntent pi = PendingIntent.getService(ctxt, widgetId, i, 0);
    am.cancel(pi);
    am.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + delay, update_interval, pi);
    // Log.i(Main.TAG, "start notification every " + update_interval / 1000
    // + "s");
}

From source file:com.phonemetra.account.util.AccountUtils.java

public static void scheduleRetry(Context context, SharedPreferences prefs, Intent intent) {
    int backoffTimeMs = getBackoff(prefs);
    int nextAttempt = backoffTimeMs / 2 + sRandom.nextInt(backoffTimeMs);
    if (Account.DEBUG)
        Log.d(TAG, "Scheduling retry, backoff = " + nextAttempt + " (" + backoffTimeMs + ") for "
                + intent.getAction());//from   w w w .j  av  a 2  s.  c  o m
    PendingIntent retryPendingIntent = PendingIntent.getService(context, 0, intent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    am.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + nextAttempt, retryPendingIntent);
    if (backoffTimeMs < Account.MAX_BACKOFF_MS) {
        setBackoff(prefs, backoffTimeMs * 2);
    }
}

From source file:net.peterkuterna.android.apps.devoxxfrsched.service.CfpSyncManager.java

/**
 * Set up the alarm.// w  w  w .  j  a  v  a 2  s  . c o  m
 * 
 * @param trigger
 *            when the alarm should first trigger in milliseconds.
 */
public void setSyncAlarm(long trigger) {
    Log.d(TAG, "Setting up sync alarm");

    final Context context = getContext();
    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    final boolean doBackgroundUpdates = prefs.getBoolean(SettingsActivity.KEY_BACKGROUND_UPDATES, true);
    if (doBackgroundUpdates) {
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        PendingIntent pi = getAlarmPendingIntent();
        if (pi == null) {
            pi = createAlarmPendingIntent();
        }
        am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + trigger, INTERVAL,
                pi);
    }
}

From source file:damo.three.ie.prepay.ConnectivityReceiver.java

@Override
public void onReceive(Context context, Intent intent) {

    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);

    if (sharedPrefs.getBoolean("backgroundupdate", true) && !(sharedPrefs.getString("mobile", "").equals("")
            && sharedPrefs.getString("password", "").equals(""))) {

        Log.d(Constants.TAG, "Internet back!! updating usage!");

        Intent receiver = new Intent(context, UpdateReceiver.class);
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        // Using different request code to 0 so it won't conflict main repeating alarm.
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 2, receiver,
                PendingIntent.FLAG_UPDATE_CURRENT);
        // Keeping efficiency in mind:
        // http://developer.android.com/reference/android/app/AlarmManager.html#ELAPSED_REALTIME
        am.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), pendingIntent);

        // Disable receiver after we scheduled an update.
        disableReceiver(context);//w  w  w .  ja  v  a 2 s  .  co m
    }
}