Example usage for android.content Context ALARM_SERVICE

List of usage examples for android.content Context ALARM_SERVICE

Introduction

In this page you can find the example usage for android.content Context ALARM_SERVICE.

Prototype

String ALARM_SERVICE

To view the source code for android.content Context ALARM_SERVICE.

Click Source Link

Document

Use with #getSystemService(String) to retrieve a android.app.AlarmManager for receiving intents at a time of your choosing.

Usage

From source file:com.psiphon3.psiphonlibrary.UpgradeChecker.java

/**
 * Creates the periodic alarm used to check for updates. Can be called unconditionally; it
 * handles cases when the alarm is already created.
 * @param appContext The application context.
 *///from w ww . ja  v  a2  s  .  co  m
private static void createAlarm(Context appContext) {
    if (!allowedToSelfUpgrade(appContext)) {
        // Don't waste resources with an alarm if we can't possibly self-upgrade.
        log(appContext, R.string.upgrade_checker_no_alarm_no_selfupgrading, MyLog.Sensitivity.NOT_SENSITIVE,
                Log.WARN);
        return;
    }

    Intent intent = new Intent(appContext, UpgradeChecker.class);
    intent.setAction(ALARM_INTENT_ACTION);

    boolean alarmExists = (PendingIntent.getBroadcast(appContext, ALARM_INTENT_REQUEST_CODE, intent,
            PendingIntent.FLAG_NO_CREATE) != null);

    if (alarmExists) {
        log(appContext, R.string.upgrade_checker_alarm_exists, MyLog.Sensitivity.NOT_SENSITIVE, Log.WARN);
        return;
    }

    log(appContext, R.string.upgrade_checker_creating_alarm, MyLog.Sensitivity.NOT_SENSITIVE, Log.WARN);

    PendingIntent alarmIntent = PendingIntent.getBroadcast(appContext, ALARM_INTENT_REQUEST_CODE, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager alarmMgr = (AlarmManager) appContext.getSystemService(Context.ALARM_SERVICE);
    alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
            SystemClock.elapsedRealtime() + AlarmManager.INTERVAL_FIFTEEN_MINUTES,
            AlarmManager.INTERVAL_HALF_DAY, alarmIntent);
}

From source file:com.nextgis.mobile.services.TrackerService.java

protected void ScheduleNextUpdate(Context context, String action, long nMinTimeBetweenSend,
        boolean bEnergyEconomy, boolean bStart) {
    if (context == null)
        return;//from w ww  . ja  va2  s . c  o  m
    Log.d(TAG, "Schedule Next Update for tracker " + bStart);
    if (bStart == false)
        return;
    Intent intent = new Intent(action);
    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:nerd.tuxmobil.fahrplan.congress.FahrplanMisc.java

public static void addAlarm(Context context, Lecture lecture, int alarmTimesIndex) {
    int[] alarm_times = { 0, 5, 10, 15, 30, 45, 60 };
    long when;/* ww  w .  j a v a  2s.  com*/
    Time time;
    long startTime;
    long startTimeInSeconds = lecture.dateUTC;

    if (startTimeInSeconds > 0) {
        when = startTimeInSeconds;
        startTime = startTimeInSeconds;
        time = new Time();
    } else {
        time = lecture.getTime();
        startTime = time.normalize(true);
        when = time.normalize(true);
    }
    long alarmTimeDiffInSeconds = alarm_times[alarmTimesIndex] * 60 * 1000;
    when -= alarmTimeDiffInSeconds;

    // DEBUG
    // when = System.currentTimeMillis() + (30 * 1000);

    time.set(when);
    MyApp.LogDebug("addAlarm", "Alarm time: " + time.format("%Y-%m-%dT%H:%M:%S%z") + ", in seconds: " + when);

    Intent intent = new Intent(context, AlarmReceiver.class);
    intent.putExtra(BundleKeys.ALARM_LECTURE_ID, lecture.lecture_id);
    intent.putExtra(BundleKeys.ALARM_DAY, lecture.day);
    intent.putExtra(BundleKeys.ALARM_TITLE, lecture.title);
    intent.putExtra(BundleKeys.ALARM_START_TIME, startTime);
    intent.setAction(AlarmReceiver.ALARM_LECTURE);

    intent.setData(Uri.parse("alarm://" + lecture.lecture_id));

    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    PendingIntent pendingintent = PendingIntent.getBroadcast(context, Integer.parseInt(lecture.lecture_id),
            intent, 0);

    // Cancel any existing alarms for this lecture
    alarmManager.cancel(pendingintent);

    // Set new alarm
    alarmManager.set(AlarmManager.RTC_WAKEUP, when, pendingintent);

    int alarmTimeInMin = alarm_times[alarmTimesIndex];

    // write to DB

    AlarmsDBOpenHelper alarmDB = new AlarmsDBOpenHelper(context);

    SQLiteDatabase db = alarmDB.getWritableDatabase();

    // delete any previous alarms of this lecture
    try {
        db.beginTransaction();
        db.delete(AlarmsTable.NAME, AlarmsTable.Columns.EVENT_ID + "=?", new String[] { lecture.lecture_id });

        ContentValues values = new ContentValues();

        values.put(AlarmsTable.Columns.EVENT_ID, Integer.parseInt(lecture.lecture_id));
        values.put(AlarmsTable.Columns.EVENT_TITLE, lecture.title);
        values.put(AlarmsTable.Columns.ALARM_TIME_IN_MIN, alarmTimeInMin);
        values.put(AlarmsTable.Columns.TIME, when);
        DateFormat df = SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.SHORT, SimpleDateFormat.SHORT);
        values.put(AlarmsTable.Columns.TIME_TEXT, df.format(new Date(when)));
        values.put(AlarmsTable.Columns.DISPLAY_TIME, startTime);
        values.put(AlarmsTable.Columns.DAY, lecture.day);

        db.insert(AlarmsTable.NAME, null, values);
        db.setTransactionSuccessful();
    } catch (SQLException e) {
    } finally {
        db.endTransaction();
        db.close();
    }

    lecture.has_alarm = true;
}

From source file:com.nextgis.mobile.TrackerService.java

protected void ScheduleNextUpdate(Context context, String action, long nMinTimeBetweenSend,
        boolean bEnergyEconomy, boolean bStart) {
    if (context == null)
        return;/*www  .j a va  2 s  .  c o  m*/
    Log.d(MainActivity.TAG, "Schedule Next Update for tracker " + bStart);
    if (bStart == false)
        return;
    Intent intent = new Intent(action);
    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:com.atlas.mars.weatherradar.MainActivity.java

void morningAlarm() {

    //todo  ?   ??
    //startService(new Intent(this, MorningService.class));

    long time = db.getMorningWakeUp();
    alarmManagerMorning = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    morningIntent = createIntent("morningAction", "extraMorning", MorningBroadCast.class);
    startService(morningIntent);/*from   w w w  .  j  av  a  2 s.co  m*/
    pIntent2 = PendingIntent.getBroadcast(this, 0, morningIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    alarmManagerMorning.cancel(pIntent2);

    alarmManagerMorning.set(AlarmManager.RTC_WAKEUP, time, pIntent2);
    //todo ?   ?  ??
    // alarmManagerMorning.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+1*1000, pIntent2);
}

From source file:org.simlar.SimlarService.java

private void startKeepAwake() {
    final Intent startIntent = new Intent("org.simlar.keepAwake");
    mkeepAwakePendingIntent = PendingIntent.getBroadcast(this, 0, startIntent, 0);

    ((AlarmManager) getSystemService(Context.ALARM_SERVICE)).setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
            SystemClock.elapsedRealtime() + 600000, 600000, mkeepAwakePendingIntent);

    IntentFilter filter = new IntentFilter();
    filter.addAction("org.simlar.keepAwake");
    registerReceiver(mKeepAwakeReceiver, filter);
}

From source file:com.jjcamera.apps.iosched.service.SessionAlarmService.java

private void scheduleAlarm(final long sessionStart, final long sessionEnd, final long alarmOffset) {

    NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    nm.cancel(NOTIFICATION_ID);//from   w  w  w. jav  a2s  .  c  o  m
    final long currentTime = UIUtils.getCurrentTime(this);
    // If the session is already started, do not schedule system notification.
    if (currentTime > sessionStart) {
        LOGD(TAG, "Not scheduling alarm because target time is in the past: " + sessionStart);
        return;
    }

    // By default, sets alarm to go off at 10 minutes before session start time.  If alarm
    // offset is provided, alarm is set to go off by that much time from now.
    long alarmTime;
    if (alarmOffset == UNDEFINED_ALARM_OFFSET) {
        alarmTime = sessionStart - MILLI_TEN_MINUTES;
    } else {
        alarmTime = currentTime + alarmOffset;
    }

    LOGD(TAG, "Scheduling alarm for " + alarmTime + " = " + (new Date(alarmTime)).toString());

    final Intent notifIntent = new Intent(ACTION_NOTIFY_SESSION, null, this, SessionAlarmService.class);
    // Setting data to ensure intent's uniqueness for different session start times.
    notifIntent.setData(new Uri.Builder().authority("com.jjcamera.apps.iosched")
            .path(String.valueOf(sessionStart)).build());
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_START, sessionStart);
    LOGD(TAG, "-> Intent extra: session start " + sessionStart);
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_END, sessionEnd);
    LOGD(TAG, "-> Intent extra: session end " + sessionEnd);
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_ALARM_OFFSET, alarmOffset);
    LOGD(TAG, "-> Intent extra: session alarm offset " + alarmOffset);
    PendingIntent pi = PendingIntent.getService(this, 0, notifIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    final AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    // Schedule an alarm to be fired to notify user of added sessions are about to begin.
    LOGD(TAG, "-> Scheduling RTC_WAKEUP alarm at " + alarmTime);
    am.set(AlarmManager.RTC_WAKEUP, alarmTime, pi);
}

From source file:org.videolan.vlc.gui.dialogs.AdvOptionsDialog.java

public static void setSleep(Calendar time) {
    AlarmManager alarmMgr = (AlarmManager) VLCApplication.getAppContext()
            .getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(VLCApplication.SLEEP_INTENT);
    PendingIntent sleepPendingIntent = PendingIntent.getBroadcast(VLCApplication.getAppContext(), 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    if (time != null) {
        alarmMgr.set(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), sleepPendingIntent);
    } else {/*from   w  w w .ja v  a 2 s.com*/
        alarmMgr.cancel(sleepPendingIntent);
    }
    VLCApplication.sPlayerSleepTime = time;
}

From source file:com.ksk.droidbatterybooster.provider.TimeSchedule.java

/**
 * Sets action in AlarmManger.  This is what will
 * actually launch the action when the schedule triggers.
 *
 * @param schedule TimeSchedule./*  w  ww . ja  v a  2  s .  c  o  m*/
 * @param atTimeInMillis milliseconds since epoch
 */
@SuppressLint("NewApi")
private static void enableAction(Context context, final TimeSchedule schedule, final long atTimeInMillis) {

    if (Log.LOGV) {
        Log.v("** setSchedule id " + schedule.id + " atTime " + atTimeInMillis);
    }

    Intent intent = new Intent(EXECUTE_SCHEDULE_ACTION);

    // XXX: This is a slight hack to avoid an exception in the remote
    // AlarmManagerService process. The AlarmManager adds extra data to
    // this Intent which causes it to inflate. Since the remote process
    // does not know about the TimeSchedule class, it throws a
    // ClassNotFoundException.
    //
    // To avoid this, we marshall the data ourselves and then parcel a plain
    // byte[] array. The ScheduleReceiver class knows to build the TimeSchedule
    // object from the byte[] array.
    Parcel out = Parcel.obtain();
    schedule.writeToParcel(out, 0);
    out.setDataPosition(0);
    intent.putExtra(INTENT_RAW_DATA, out.marshall());

    PendingIntent sender = PendingIntent.getBroadcast(context, schedule.hashCode(), intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    if (Utils.isKitKatOrLater()) {
        am.setExact(AlarmManager.RTC_WAKEUP, atTimeInMillis, sender);
    } else {
        am.set(AlarmManager.RTC_WAKEUP, atTimeInMillis, sender);
    }

    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(atTimeInMillis);
    String timeString = formatDayAndTime(context, c);
    saveNextAlarm(context, timeString);
}