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.google.samples.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);/* www. ja  va  2  s  . 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.google.samples.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:com.razza.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  . j  a va  2s  . c  o  m
    final long currentTime = UIUtils.getCurrentTime(this);
    // If the session is already started, do not schedule system notification.
    if (currentTime > sessionStart) {
        LogUtils.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;
    }

    LogUtils.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.razza.apps.iosched").path(String.valueOf(sessionStart)).build());
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_START, sessionStart);
    LogUtils.LOGD(TAG, "-> Intent extra: session start " + sessionStart);
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_END, sessionEnd);
    LogUtils.LOGD(TAG, "-> Intent extra: session end " + sessionEnd);
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_ALARM_OFFSET, alarmOffset);
    LogUtils.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.
    LogUtils.LOGD(TAG, "-> Scheduling RTC_WAKEUP alarm at " + alarmTime);
    am.set(AlarmManager.RTC_WAKEUP, alarmTime, pi);
}

From source file:com.saarang.samples.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);/* ww  w  .j a  va2s .  c om*/
    final long currentTime = UIUtils.getCurrentTime(this);
    // If the session is already started, do not schedule system notification.
    if (currentTime > sessionStart) {
        LogUtils.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;
    }

    LogUtils.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.saarang.samples.apps.iosched")
            .path(String.valueOf(sessionStart)).build());
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_START, sessionStart);
    LogUtils.LOGD(TAG, "-> Intent extra: session start " + sessionStart);
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_END, sessionEnd);
    LogUtils.LOGD(TAG, "-> Intent extra: session end " + sessionEnd);
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_ALARM_OFFSET, alarmOffset);
    LogUtils.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.
    LogUtils.LOGD(TAG, "-> Scheduling RTC_WAKEUP alarm at " + alarmTime);
    am.set(AlarmManager.RTC_WAKEUP, alarmTime, pi);
}

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

/**
 * @param id Schedule ID./*from w  ww  .jav a2 s .com*/
 * 
 */
static void disableAction(Context context) {
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    PendingIntent sender = PendingIntent.getBroadcast(context, 0, new Intent(EXECUTE_SCHEDULE_ACTION),
            PendingIntent.FLAG_CANCEL_CURRENT);
    am.cancel(sender);
    saveNextAlarm(context, "");
}

From source file:com.atlas.mars.weatherradar.MainActivity.java

void alarmOn() {
    am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    intent1 = createIntent("action 1", "extra 1", SampleBootReceiver.class);
    pIntent1 = PendingIntent.getBroadcast(this, 0, intent1, PendingIntent.FLAG_CANCEL_CURRENT);
    am.cancel(pIntent1);/*w w w  .  ja v a 2 s.com*/

    //todo ??
    //am.set(AlarmManager.RTC_WAKEUP, startAlarm, pIntent1);
    am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 1 * 1000, pIntent1);
}

From source file:org.proninyaroslav.libretorrent.services.TorrentTaskService.java

@Override
public void onTaskRemoved(Intent rootIntent) {
    if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
        Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass());
        restartServiceIntent.setPackage(getPackageName());

        PendingIntent restartServicePendingIntent = PendingIntent.getService(getApplicationContext(), 1,
                restartServiceIntent, PendingIntent.FLAG_ONE_SHOT);

        AlarmManager alarmService = (AlarmManager) getApplicationContext()
                .getSystemService(Context.ALARM_SERVICE);
        alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 1000,
                restartServicePendingIntent);
    }//www  .  j  a  v  a2 s .  c  o m
}

From source file:com.sean.takeastand.storage.ScheduleEditor.java

private void setDailyRepeatingAlarm(int UID, String time) {
    Intent intent = new Intent(mContext, StartScheduleReceiver.class);
    intent.putExtra(Constants.ALARM_UID, UID);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, UID, intent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    ((AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE)).setRepeating(AlarmManager.RTC_WAKEUP,
            nextAlarmTime(time).getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
    Log.i(TAG, "Set Daily Repeating alarm for " + Long.toString(nextAlarmTime(time).getTimeInMillis())
            + " current time " + Long.toString(System.currentTimeMillis()));
}

From source file:com.kyleszombathy.sms_scheduler.MessageAlarmReceiver.java

/** Method to set a new alarm
 * @param context The app context//from   w ww. ja  v a  2s .c om
 * @param timeToSend The Time to send the message (in Calendar format)
 * @param phoneNumberList String Arraylist of phone numbers
 * @param messageContent The content of the message you want to send
 * @param alarmNumber Provide an identifier for the alarm
 * @param nameList The list of names, corresponding with the phone numbers*/
public void createAlarm(Context context, Calendar timeToSend, ArrayList<String> phoneNumberList,
        String messageContent, int alarmNumber, ArrayList<String> nameList) {
    this.context = context;

    // Creates new alarm
    alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intentAlarm = new Intent(context, MessageAlarmReceiver.class);

    // Add extras
    Bundle extras = new Bundle();
    extras.putStringArrayList("pNum", phoneNumberList);
    extras.putString("message", messageContent);
    extras.putInt("alarmNumber", alarmNumber);
    extras.putStringArrayList("nameList", nameList);
    intentAlarm.putExtras(extras);

    // Set alarm
    pendingIntent = PendingIntent.getBroadcast(context, alarmNumber, intentAlarm,
            PendingIntent.FLAG_UPDATE_CURRENT);
    alarm.set(AlarmManager.RTC_WAKEUP, timeToSend.getTimeInMillis(), pendingIntent);

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

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

    Log.i(TAG, "createAlarm: Alarm Created. alarmNumber: " + alarmNumber);
}

From source file:com.granita.tasks.notification.NotificationActionUtils.java

/**
 * Cancels the undo timeout for a notification action. This should be called if the undo notification is clicked (to prevent the action from being performed
 * anyway) or cleared (since we have already performed the action).
 *///w w  w.  ja  v  a  2 s .  com
public static void cancelUndoTimeout(final Context context, final NotificationAction notificationAction) {
    final AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    final PendingIntent pendingIntent = createUndoTimeoutPendingIntent(context, notificationAction);
    alarmManager.cancel(pendingIntent);
}

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

protected void ScheduleNextUpdate(Context context, int nCommand, long nMinTimeBetweenSend,
        boolean bEnergyEconomy) {
    if (context == null)
        return;//ww  w . j a va  2s  .  com

    Intent intent = new Intent(MainActivity.INTENT_NAME);
    intent.putExtra(RECEIVER, mUserNasaReceiver);
    intent.putExtra(RECEIVER_SCANEX, mScanexReceiver);
    intent.putExtra(COMMAND, nCommand);
    intent.putExtra(SOURCE, mnFilter);

    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);
}