Example usage for android.app AlarmManager set

List of usage examples for android.app AlarmManager set

Introduction

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

Prototype

public void set(@AlarmType int type, long triggerAtMillis, PendingIntent operation) 

Source Link

Document

Schedule an alarm.

Usage

From source file:gov.whitehouse.services.LiveService.java

private void scheduleNextUpdate() {
    Intent intent = new Intent(this, this.getClass());
    PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    long now = System.currentTimeMillis();
    int intervalSeconds = getResources().getInteger(R.integer.live_feed_update_interval_seconds);
    long intervalMillis = intervalSeconds * DateUtils.SECOND_IN_MILLIS;
    long nextUpdateTimeMillis = now + intervalMillis;

    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC, nextUpdateTimeMillis, pendingIntent);
}

From source file:Main.java

public static <T extends BroadcastReceiver> void scheduleUpdate(Context context, Class<T> clazz) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, clazz);
    PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    Random random = new Random(System.currentTimeMillis());
    long offset = random.nextLong() % (12 * 60 * 60 * 1000);
    long interval = (24 * 60 * 60 * 1000) + offset;
    String prefKey = "pref_scheduled_monitor_config_update_" + clazz.getCanonicalName();
    long scheduledTime = preferences.getLong(prefKey, -1);

    if (scheduledTime == -1) {
        context.sendBroadcast(intent);//from  www  . j  av  a2 s.  c o m
    }

    if (scheduledTime <= System.currentTimeMillis()) {
        context.sendBroadcast(intent);
        scheduledTime = System.currentTimeMillis() + interval;
        preferences.edit().putLong(prefKey, scheduledTime).commit();
        Log.w("PeriodicActionUtils",
                "Scheduling for all new time: " + scheduledTime + " (" + clazz.getSimpleName() + ")");
    } else {
        Log.w("PeriodicActionUtils", "Scheduling for time found in preferences: " + scheduledTime + " ("
                + clazz.getSimpleName() + ")");
    }

    am.cancel(sender);
    am.set(AlarmManager.RTC_WAKEUP, scheduledTime, sender);

    Log.w("PeriodicActionUtils", "Scheduled for: " + scheduledTime + " (" + clazz.getSimpleName() + ")");
}

From source file:com.ota.updates.utils.Utils.java

public static void scheduleNotification(Context context, boolean cancel) {
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, AppReceiver.class);
    intent.setAction(START_UPDATE_CHECK);
    int intentId = 1673;
    int intentFlag = PendingIntent.FLAG_UPDATE_CURRENT;

    if (cancel) {
        if (alarmManager != null) {
            if (DEBUGGING)
                Log.d(TAG, "Cancelling alarm");
            alarmManager.cancel(PendingIntent.getBroadcast(context, intentId, intent, intentFlag));
        }// w  w w  .  ja  v  a2  s  . c om
    } else {
        int requestedInterval;

        if (DEBUG_NOTIFICATIONS) {
            requestedInterval = 30000;
        } else {
            requestedInterval = Preferences.getBackgroundFrequency(context);
        }

        if (DEBUGGING)
            Log.d(TAG, "Setting alarm for " + requestedInterval + " seconds");
        Calendar calendar = Calendar.getInstance();
        long time = calendar.getTimeInMillis() + requestedInterval * 1000;
        alarmManager.set(AlarmManager.RTC_WAKEUP, time,
                PendingIntent.getBroadcast(context, intentId, intent, intentFlag));
    }
}

From source file:step.StepService.java

private void startAutoAlarm() {
    //???, ?AutoStarter
    Intent intent = new Intent(StepService.this, AutoStarter.class);
    intent.setAction("com.renren.mobile.android.ACTION_AUTOSTARTER");
    PendingIntent autoStart = PendingIntent.getBroadcast(StepService.this, 0, intent, 0);

    //?/*from   w  w  w .j ava  2 s . c  o  m*/
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.add(Calendar.SECOND, 5);

    AlarmManager alarmManager = (AlarmManager) StepManager.getmContext()
            .getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), autoStart);
}

From source file:com.aafr.alfonso.sunshine.app.ForecastFragment.java

private void updateWeather() {
    Intent alarmIntent = new Intent(getActivity(), SunshineService.AlarmReceiver.class);
    alarmIntent.putExtra(SunshineService.LOCATION_QUERY_EXTRA, Utility.getPreferredLocation(getActivity()));

    //Wrap in a pending intent which only fires once.
    PendingIntent pi = PendingIntent.getBroadcast(getActivity(), 0, alarmIntent, PendingIntent.FLAG_ONE_SHOT);//getBroadcast(context, 0, i, 0);

    AlarmManager am = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);

    //Set the AlarmManager to wake up the system.
    am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000, pi);
}

From source file:se.hyperlab.tigcm.TiGCMModule.java

@Kroll.method
public void schedule(long time, HashMap data) {
    TiApplication app = TiApplication.getInstance();
    int ntfId = app.getAppProperties().getInt(PROPERTY_NOTIFICATION_COUNTER, 0);

    Log.d(TAG, "Scheduling notification " + ntfId + " at " + time);

    Intent intent = new Intent(app.getApplicationContext(), NotificationPublisher.class);
    intent.putExtra(PROPERTY_NOTIFICATION_DATA, data);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(app.getApplicationContext(), ntfId, intent,
            PendingIntent.FLAG_ONE_SHOT);

    AlarmManager alarmManager = (AlarmManager) app.getApplicationContext()
            .getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, time, pendingIntent);

    app.getAppProperties().setInt(PROPERTY_NOTIFICATION_COUNTER, ntfId + 1);
}

From source file:me.mcmadbat.laststats.MainActivity.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    //TODO: find a better way to change users
    if (id == R.id.Change_User) {
        user.delete();/*from  w  w  w .java  2s . c  o  m*/
        Toast t = Toast.makeText(getApplicationContext(), "Please enter user again", Toast.LENGTH_SHORT);
        t.show();

        Intent mStartActivity = new Intent(getApplicationContext(), MainActivity.class);
        int mPendingIntentId = 123456;
        PendingIntent mPendingIntent = PendingIntent.getActivity(getApplicationContext(), mPendingIntentId,
                mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager mgr = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
        mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
        System.exit(0);

        return true;
    }

    return super.onOptionsItemSelected(item);
}

From source file:com.nextgis.firereporter.ReporterService.java

public void ScheduleNextUpdate(Context context, long nMinTimeBetweenSend, boolean bEnergyEconomy) {
    if (context == null)
        return;//from w ww . ja v a2  s . c o  m

    Intent intent = new Intent(ReporterService.ACTION_START);
    PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    // The update frequency should often be user configurable.  This is not.

    long currentTimeMillis = System.currentTimeMillis();
    long nextUpdateTimeMillis = currentTimeMillis + nMinTimeBetweenSend;
    Time nextUpdateTime = new Time();
    nextUpdateTime.set(nextUpdateTimeMillis);

    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    if (bEnergyEconomy)
        alarmManager.set(AlarmManager.RTC, nextUpdateTimeMillis, pendingIntent);
    else
        alarmManager.set(AlarmManager.RTC_WAKEUP, nextUpdateTimeMillis, pendingIntent);
}

From source file:org.fdroid.enigtext.service.KeyCachingService.java

private void startTimeoutIfAppropriate() {
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    boolean timeoutEnabled = sharedPreferences
            .getBoolean(ApplicationPreferencesActivity.PASSPHRASE_TIMEOUT_PREF, false);

    if ((activitiesRunning == 0) && (this.masterSecret != null) && timeoutEnabled && !isPassphraseDisabled()) {
        long timeoutMinutes = sharedPreferences
                .getInt(ApplicationPreferencesActivity.PASSPHRASE_TIMEOUT_INTERVAL_PREF, 60 * 5);
        long timeoutMillis = timeoutMinutes * 60 * 1000;

        Log.w("KeyCachingService", "Starting timeout: " + timeoutMillis);

        AlarmManager alarmManager = (AlarmManager) this.getSystemService(ALARM_SERVICE);
        alarmManager.cancel(pending);/*from www.  j ava 2 s  .c  om*/
        alarmManager.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + timeoutMillis, pending);
    }
}

From source file:com.google.android.gcm.GCMBaseIntentService.java

private void handleRegistration(final Context context, Intent intent) {
    String registrationId = intent.getStringExtra(EXTRA_REGISTRATION_ID);
    String error = intent.getStringExtra(EXTRA_ERROR);
    String unregistered = intent.getStringExtra(EXTRA_UNREGISTERED);
    Log.d(TAG, "handleRegistration: registrationId = " + registrationId + ", error = " + error
            + ", unregistered = " + unregistered);

    // registration succeeded
    if (registrationId != null) {
        GCMRegistrar.resetBackoff(context);
        GCMRegistrar.setRegistrationId(context, registrationId);
        onRegistered(context, registrationId);
        return;//from  w  w  w. j a  va 2s  .  co m
    }

    // unregistration succeeded
    if (unregistered != null) {
        // Remember we are unregistered
        GCMRegistrar.resetBackoff(context);
        String oldRegistrationId = GCMRegistrar.clearRegistrationId(context);
        onUnregistered(context, oldRegistrationId);
        return;
    }

    // last operation (registration or unregistration) returned an error;
    Log.d(TAG, "Registration error: " + error);
    // Registration failed
    if (ERROR_SERVICE_NOT_AVAILABLE.equals(error)) {
        boolean retry = onRecoverableError(context, error);
        if (retry) {
            int backoffTimeMs = GCMRegistrar.getBackoff(context);
            int nextAttempt = backoffTimeMs / 2 + sRandom.nextInt(backoffTimeMs);
            Log.d(TAG, "Scheduling registration retry, backoff = " + nextAttempt + " (" + backoffTimeMs + ")");
            Intent retryIntent = new Intent(INTENT_FROM_GCM_LIBRARY_RETRY);
            retryIntent.putExtra(EXTRA_TOKEN, TOKEN);
            PendingIntent retryPendingIntent = PendingIntent.getBroadcast(context, 0, retryIntent, 0);
            AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            am.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + nextAttempt,
                    retryPendingIntent);
            // Next retry should wait longer.
            if (backoffTimeMs < MAX_BACKOFF_MS) {
                GCMRegistrar.setBackoff(context, backoffTimeMs * 2);
            }
        } else {
            Log.d(TAG, "Not retrying failed operation");
        }
    } else {
        // Unrecoverable error, notify app
        onError(context, error);
    }
}