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:com.battlelancer.seriesguide.appwidget.ListWidgetProvider.java

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    super.onUpdate(context, appWidgetManager, appWidgetIds);

    // update all added list widgets
    for (int appWidgetId : appWidgetIds) {
        onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, null);
    }//from  www.  java 2  s  .  c om

    // set an alarm to update widgets every x mins if the device is awake
    Intent update = new Intent(UPDATE);
    PendingIntent pi = PendingIntent.getBroadcast(context, 195, update, 0);

    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    am.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + REPETITION_INTERVAL,
            REPETITION_INTERVAL, pi);
}

From source file:be.ac.ucl.lfsab1509.llncampus.services.AlarmService.java

@Override
public void onCreate() {
    super.onCreate();
    // Define the Context here, otherwise no Context before launching the application.
    LLNCampus.setContext(getApplicationContext());
    am = (AlarmManager) LLNCampus.getContext().getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(LLNCampus.getContext(), AlarmService.class);
    PendingIntent pi = PendingIntent.getBroadcast(LLNCampus.getContext(), 0, i, 0);
    am.set(AlarmManager.ELAPSED_REALTIME, TIME_TO_REPEAT, pi);
}

From source file:tv.acfun.a63.service.PushService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (AcApp.isMentionEnabled() && AcApp.getUser() != null) {
        mAManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        Intent i = new Intent(ACTION_REFRESH);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
        long interval = AcApp.getPreferenceRefreshingInterval();
        long triggerAtTime = SystemClock.elapsedRealtime() + interval;
        mAManager.setRepeating(AlarmManager.ELAPSED_REALTIME, triggerAtTime, interval, pendingIntent);
        if (BuildConfig.DEBUG)
            Log.i("PUSH", "start : id=" + startId + ", interval=" + interval);
    } else {//from   ww  w .  j  ava  2 s. c o  m
        // ??
        stopSelf();
    }
    return super.onStartCommand(intent, flags, startId);
}

From source file:cn.studyjams.s2.sj0119.NForget.AlarmReceiver.java

public void setAlarm(Context context, Calendar calendar, int ID) {
    mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    // Put Reminder ID in Intent Extra
    Intent intent = new Intent(context, AlarmReceiver.class);
    intent.putExtra(ReminderEditActivity.EXTRA_REMINDER_ID, Integer.toString(ID));
    mPendingIntent = PendingIntent.getBroadcast(context, ID, intent, PendingIntent.FLAG_CANCEL_CURRENT);

    // Calculate notification time
    Calendar c = Calendar.getInstance();
    long currentTime = c.getTimeInMillis();
    long diffTime = calendar.getTimeInMillis() - currentTime;

    // Start alarm using notification time
    mAlarmManager.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + diffTime, mPendingIntent);

    // Restart alarm if device is rebooted
    ComponentName receiver = new ComponentName(context, BootReceiver.class);
    PackageManager pm = context.getPackageManager();
    pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
            PackageManager.DONT_KILL_APP);
}

From source file:com.example.android.repeatingalarm.RepeatingAlarmFragment.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == R.id.sample_action) {

        // BEGIN_INCLUDE (intent_fired_by_alarm)
        // First create an intent for the alarm to activate.
        // This code simply starts an Activity, or brings it to the front if it has already
        // been created.
        Intent intent = new Intent(getActivity(), MainActivity.class);
        intent.setAction(Intent.ACTION_MAIN);
        intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        // END_INCLUDE (intent_fired_by_alarm)

        // BEGIN_INCLUDE (pending_intent_for_alarm)
        // Because the intent must be fired by a system service from outside the application,
        // it's necessary to wrap it in a PendingIntent.  Providing a different process with
        // a PendingIntent gives that other process permission to fire the intent that this
        // application has created.
        // Also, this code creates a PendingIntent to start an Activity.  To create a
        // BroadcastIntent instead, simply call getBroadcast instead of getIntent.
        PendingIntent pendingIntent = PendingIntent.getActivity(getActivity(), REQUEST_CODE, intent, 0);

        // END_INCLUDE (pending_intent_for_alarm)

        // BEGIN_INCLUDE (configure_alarm_manager)
        // There are two clock types for alarms, ELAPSED_REALTIME and RTC.
        // ELAPSED_REALTIME uses time since system boot as a reference, and RTC uses UTC (wall
        // clock) time.  This means ELAPSED_REALTIME is suited to setting an alarm according to
        // passage of time (every 15 seconds, 15 minutes, etc), since it isn't affected by
        // timezone/locale.  RTC is better suited for alarms that should be dependant on current
        // locale.

        // Both types have a WAKEUP version, which says to wake up the device if the screen is
        // off.  This is useful for situations such as alarm clocks.  Abuse of this flag is an
        // efficient way to skyrocket the uninstall rate of an application, so use with care.
        // For most situations, ELAPSED_REALTIME will suffice.
        int alarmType = AlarmManager.ELAPSED_REALTIME;
        final int FIFTEEN_SEC_MILLIS = 15000;

        // The AlarmManager, like most system services, isn't created by application code, but
        // requested from the system.
        AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(getActivity().ALARM_SERVICE);

        // setRepeating takes a start delay and period between alarms as arguments.
        // The below code fires after 15 seconds, and repeats every 15 seconds.  This is very
        // useful for demonstration purposes, but horrendous for production.  Don't be that dev.
        alarmManager.setRepeating(alarmType, SystemClock.elapsedRealtime() + FIFTEEN_SEC_MILLIS,
                FIFTEEN_SEC_MILLIS, pendingIntent);
        // END_INCLUDE (configure_alarm_manager);
        Log.i("RepeatingAlarmFragment", "Alarm set.");
    }//w  w w  . j a  va2 s. c  om
    return true;
}

From source file:com.appsaur.tarucassist.AutomuteAlarmReceiver.java

public void setAlarm(Context context, Calendar calendar, int ID) {
    mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    // Put Reminder ID in Intent Extra
    Intent intent = new Intent(context, AutomuteAlarmReceiver.class);
    intent.putExtra(BaseActivity.EXTRA_REMINDER_ID, Integer.toString(ID));
    mPendingIntent = PendingIntent.getBroadcast(context, ID, intent, PendingIntent.FLAG_CANCEL_CURRENT);

    // Calculate notification time
    Calendar c = Calendar.getInstance();
    long currentTime = c.getTimeInMillis();
    long diffTime = calendar.getTimeInMillis() - currentTime;

    // Start alarm using notification time
    mAlarmManager.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + diffTime, mPendingIntent);

    // Restart alarm if device is rebooted
    ComponentName receiver = new ComponentName(context, BootReceiver.class);
    PackageManager pm = context.getPackageManager();
    pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
            PackageManager.DONT_KILL_APP);
}

From source file:com.jtechme.apphub.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  ww  w  . j ava2  s  .  com
    if (interval > 0) {
        alarm.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 5000,
                AlarmManager.INTERVAL_HOUR, pending);
        Utils.debugLog(TAG, "Update scheduler alarm set");
    } else {
        Utils.debugLog(TAG, "Update scheduler alarm not set");
    }

}

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

public static void scheduleCMAccountPing(Context context, Intent intent) {
    if (CMAccount.DEBUG)
        Log.d(TAG,//from   ww  w . j a va  2 s. c o  m
                "Scheduling CMAccount ping, starting = "
                        + new Timestamp(SystemClock.elapsedRealtime() + AlarmManager.INTERVAL_DAY)
                        + " interval (" + AlarmManager.INTERVAL_DAY + ")");
    PendingIntent reRegisterPendingIntent = PendingIntent.getService(context, 0, intent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME,
            SystemClock.elapsedRealtime() + AlarmManager.INTERVAL_DAY, AlarmManager.INTERVAL_DAY,
            reRegisterPendingIntent);
}

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

public static void scheduleAccountPing(Context context, Intent intent) {
    if (Account.DEBUG)
        Log.d(TAG,//from w w  w  . j ava2  s.c o  m
                "Scheduling Account ping, starting = "
                        + new Timestamp(SystemClock.elapsedRealtime() + AlarmManager.INTERVAL_DAY)
                        + " interval (" + AlarmManager.INTERVAL_DAY + ")");
    PendingIntent reRegisterPendingIntent = PendingIntent.getService(context, 0, intent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME,
            SystemClock.elapsedRealtime() + AlarmManager.INTERVAL_DAY, AlarmManager.INTERVAL_DAY,
            reRegisterPendingIntent);
}

From source file:com.github.sryze.wirebug.DebugStatusService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent != null && intent.getAction() != null) {
        if (intent.getAction().equals(ACTION_UPDATE_STATUS)) {
            updateStatus();//from   w  w  w. j av  a  2s  .  c o m
            Intent updateStatusIntent = new Intent(DebugStatusService.this, DebugStatusService.class);
            updateStatusIntent.setAction(ACTION_UPDATE_STATUS);
            PendingIntent alarmPendingIntent = PendingIntent.getService(DebugStatusService.this, 0,
                    updateStatusIntent, PendingIntent.FLAG_CANCEL_CURRENT);
            alarmManager.cancel(alarmPendingIntent);
            alarmManager.set(AlarmManager.ELAPSED_REALTIME,
                    SystemClock.elapsedRealtime() + STATUS_UPDATE_INTERVAL, alarmPendingIntent);
        } else if (intent.getAction().equals(ACTION_STOP)) {
            DebugManager.setTcpDebuggingEnabled(false);
            updateStatus();
        }
    }
    return START_STICKY;
}