Example usage for android.app AlarmManager RTC

List of usage examples for android.app AlarmManager RTC

Introduction

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

Prototype

int RTC

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

Click Source Link

Document

Alarm time in System#currentTimeMillis System.currentTimeMillis() (wall clock time in UTC).

Usage

From source file:Main.java

/**
 * Restart the app./* ww  w. j  a va  2 s .c o  m*/
 * @param context
 */
public static void restartApplication(Context context) {
    Intent i = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    PendingIntent p = PendingIntent.getActivity(context, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 500, p);

    //kill the application
    System.exit(0);
}

From source file:com.alchemiasoft.book.receiver.SuggestionReceiver.java

/**
 * Schedule a suggestion alarm.//from w w w  . j  av  a  2 s.  c o m
 *
 * @param context reference.
 * @return true if the alarm has been successfully scheduled, false otherwise.
 */
public static boolean scheduleSuggestion(Context context) {
    final AlarmManager alarmManager = AlarmUtil.getAlarmManager(context);
    if (sPendingIntent != null) {
        alarmManager.cancel(sPendingIntent);
    }
    final UserData userData = UserData.load(context);
    final UserData.SuggestionInterval interval = userData.suggestionInterval();
    if (interval == UserData.SuggestionInterval.NEVER) {
        return false;
    }
    final Intent intent = new Intent(SUGGESTION_ACTION);
    sPendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    final long refreshInMills = interval.seconds() * DateUtils.SECOND_IN_MILLIS;
    Log.d(TAG_LOG, "AlarmReceiver: scheduling suggestion in " + interval.seconds() + " sec(s).");
    alarmManager.set(AlarmManager.RTC, System.currentTimeMillis() + refreshInMills, sPendingIntent);
    return true;
}

From source file:org.dodgybits.shuffle.android.server.sync.SyncAlarmService.java

@Override
protected void onHandleIntent(Intent intent) {
    long lastSyncDate = Preferences.getLastSyncLocalDate(this);
    long nextSyncDate = Math.max(System.currentTimeMillis() + 5000L, lastSyncDate + SYNC_PERIOD);

    if (Log.isLoggable(TAG, Log.INFO)) {
        Log.i(TAG, "Next sync at " + new Date(nextSyncDate));
    }//from w  w  w.  j  a v a  2  s  .c o  m

    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent syncIntent = new Intent(this, SyncAlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, syncIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    alarmManager.setRepeating(AlarmManager.RTC, nextSyncDate, SYNC_PERIOD, pendingIntent);

    // Release the wake lock provided by the WakefulBroadcastReceiver
    WakefulBroadcastReceiver.completeWakefulIntent(intent);
}

From source file:com.numenta.core.service.AlarmReceiver.java

public void startAlarm(Context context) {
    _alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, AlarmReceiver.class);
    _alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

    // Tries to wakeup the phone every 15 minutes to synchronize the data.
    // The 15 minutes interval was chosen because this way this be alarm will be phase-aligned
    // with other alarms to reduce the number of wake ups. See AlarmManager#setInexactRepeating
    _alarmMgr.setInexactRepeating(AlarmManager.RTC, System.currentTimeMillis(),
            AlarmManager.INTERVAL_FIFTEEN_MINUTES, _alarmIntent);
}

From source file:com.conferenceengineer.android.iosched.gcm.command.SyncCommand.java

private void scheduleSync(Context context, int syncJitter) {
    int jitterMillis = (int) (RANDOM.nextFloat() * syncJitter);
    final String debugMessage = "Scheduling next sync for " + jitterMillis + "ms";
    LOGI(TAG, debugMessage);//  w w w . j  a  va 2  s  . com

    ((AlarmManager) context.getSystemService(Context.ALARM_SERVICE)).set(AlarmManager.RTC,
            System.currentTimeMillis() + jitterMillis, PendingIntent.getBroadcast(context, 0,
                    new Intent(context, TriggerSyncReceiver.class), PendingIntent.FLAG_CANCEL_CURRENT));

}

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.  j  a  va2 s .com*/
        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:nl.atcomputing.spacetravelagency.order.DepartureInfoService.java

static public void setupDepartureInfoServiceAlarm(Context context) {
    Intent intent = new Intent(context, DepartureInfoService.class);
    PendingIntent pi = PendingIntent.getService(context, 1, intent, 0);
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Calendar cal = Calendar.getInstance();
    am.setRepeating(AlarmManager.RTC, cal.getTimeInMillis(), 600000, pi);
}

From source file:eu.istvank.apps.lenslog.receivers.NotifyAlarmReceiver.java

/**
 * Sets a repeating alarm that runs once a day at approximately 8:30 a.m. When the
 * alarm fires, the app broadcasts an Intent to this WakefulBroadcastReceiver.
 * @param context/*from  ww  w.j a  v  a  2 s .  c  o  m*/
 */
@TargetApi(Build.VERSION_CODES.KITKAT)
public void setAlarm(Context context) {
    alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, NotifyAlarmReceiver.class);
    alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

    // get notification time from preferences
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
    long notificationTime = sp.getLong(SettingsFragment.KEY_PREF_NOTIFICATION_TIME, 50400000);

    Calendar calendar = Calendar.getInstance();
    //TODO: if the time has passed already, set it to tomorrow.
    calendar.setTimeInMillis(notificationTime);

    // The following line is for debug only
    //alarmMgr.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);

    // Set the alarm to fire according to the device's clock, and to repeat once a day.
    alarmMgr.setInexactRepeating(AlarmManager.RTC, notificationTime, AlarmManager.INTERVAL_DAY, alarmIntent);

    // Enable {@code SampleBootReceiver} to automatically restart the alarm when the
    // device is rebooted.
    ComponentName receiver = new ComponentName(context, NotifyAlarmReceiver.class);
    PackageManager pm = context.getPackageManager();

    pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
            PackageManager.DONT_KILL_APP);
}

From source file:com.capstone.transit.trans_it.RouteMap.java

@Override
protected void onResume() {
    super.onResume();
    setUpMapIfNeeded();// w  w  w  . jav a 2 s .  c  o  m
    positionsServiceIntent = new Intent(getApplicationContext(), RefreshPositionsService.class);
    positionsServiceIntent.putExtra("EXTRA_ROUTE_ID", routeID);
    positionsServiceIntent.putExtra("EXTRA_RECEIVER", new PositionsReceiver(new Handler()));
    final PendingIntent pendingIntent = PendingIntent.getService(this, 0, positionsServiceIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    long trigger = System.currentTimeMillis();
    int intervalMillis = 1000 * 60;
    AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
    alarm.setRepeating(AlarmManager.RTC, trigger, intervalMillis, pendingIntent);
}

From source file:com.google.android.apps.iosched.gcm.GCMIntentService.java

@Override
protected void onMessage(Context context, Intent intent) {
    if (UIUtils.isGoogleTV(context)) {
        // Google TV uses SyncHelper directly.
        return;//from www. j av  a2  s .  c o  m
    }
    String announcement = intent.getStringExtra("announcement");
    if (announcement != null) {
        displayNotification(context, announcement);
        return;
    }

    int jitterMillis = (int) (sRandom.nextFloat() * TRIGGER_SYNC_MAX_JITTER_MILLIS);
    final String debugMessage = "Received message to trigger sync; " + "jitter = " + jitterMillis + "ms";
    LOGI(TAG, debugMessage);

    if (BuildConfig.DEBUG) {
        displayNotification(context, debugMessage);
    }

    ((AlarmManager) context.getSystemService(ALARM_SERVICE)).set(AlarmManager.RTC,
            System.currentTimeMillis() + jitterMillis, PendingIntent.getBroadcast(context, 0,
                    new Intent(context, TriggerSyncReceiver.class), PendingIntent.FLAG_CANCEL_CURRENT));
}