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.bluros.music.MusicService.java

@Override
public void onCreate() {
    if (D)/*  w  ww  . j av  a2s  .  c om*/
        Log.d(TAG, "Creating service");
    super.onCreate();

    mNotificationManager = NotificationManagerCompat.from(this);

    // gets a pointer to the playback state store
    mPlaybackStateStore = MusicPlaybackState.getInstance(this);
    mSongPlayCount = SongPlayCount.getInstance(this);
    mRecentStore = RecentStore.getInstance(this);

    mHandlerThread = new HandlerThread("MusicPlayerHandler", android.os.Process.THREAD_PRIORITY_BACKGROUND);
    mHandlerThread.start();

    mPlayerHandler = new MusicPlayerHandler(this, mHandlerThread.getLooper());

    mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    mMediaButtonReceiverComponent = new ComponentName(getPackageName(),
            MediaButtonIntentReceiver.class.getName());
    mAudioManager.registerMediaButtonEventReceiver(mMediaButtonReceiverComponent);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        setUpMediaSession();

    mPreferences = getSharedPreferences("Service", 0);
    mCardId = getCardId();

    registerExternalStorageListener();

    mPlayer = new MultiPlayer(this);
    mPlayer.setHandler(mPlayerHandler);

    // Initialize the intent filter and each action
    final IntentFilter filter = new IntentFilter();
    filter.addAction(SERVICECMD);
    filter.addAction(TOGGLEPAUSE_ACTION);
    filter.addAction(PAUSE_ACTION);
    filter.addAction(STOP_ACTION);
    filter.addAction(SLEEP_MODE_STOP_ACTION);
    filter.addAction(NEXT_ACTION);
    filter.addAction(PREVIOUS_ACTION);
    filter.addAction(PREVIOUS_FORCE_ACTION);
    filter.addAction(REPEAT_ACTION);
    filter.addAction(SHUFFLE_ACTION);
    filter.addAction(RemoteSelectDialog.REMOTE_START_SCAN);
    filter.addAction(RemoteSelectDialog.REMOTE_STOP_SCAN);
    filter.addAction(RemoteSelectDialog.REMOTE_CONNECT);
    // Attach the broadcast listener
    registerReceiver(mIntentReceiver, filter);

    mMediaStoreObserver = new MediaStoreObserver(mPlayerHandler);
    getContentResolver().registerContentObserver(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, true,
            mMediaStoreObserver);
    getContentResolver().registerContentObserver(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, true,
            mMediaStoreObserver);

    // Initialize the wake lock
    final PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
    mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
    mWakeLock.setReferenceCounted(false);

    final Intent shutdownIntent = new Intent(this, MusicService.class);
    shutdownIntent.setAction(SHUTDOWN);

    mAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    mShutdownIntent = PendingIntent.getService(this, 0, shutdownIntent, 0);

    scheduleDelayedShutdown();

    reloadQueueAfterPermissionCheck();
    notifyChange(QUEUE_CHANGED);
    notifyChange(META_CHANGED);
}

From source file:cn.studyjams.s2.sj0132.bowenyan.mygirlfriend.nononsenseapps.notepad.data.receiver.NotificationHelper.java

/**
 * Schedules to be woken up at the next notification time.
 *///from w  ww  .  j a v  a2  s .c om
private static void scheduleNext(Context context) {
    // Get first future notification
    final Calendar now = Calendar.getInstance();
    final List<cn.studyjams.s2.sj0132.bowenyan.mygirlfriend.nononsenseapps.notepad.data.model.sql.Notification> notifications = cn.studyjams.s2.sj0132.bowenyan.mygirlfriend.nononsenseapps.notepad.data.model.sql.Notification
            .getNotificationsWithTime(context, now.getTimeInMillis(), false);

    // if not empty, schedule alarm wake up
    if (!notifications.isEmpty()) {
        // at first's time
        // Create a new PendingIntent and add it to the AlarmManager
        Intent intent = new Intent(Intent.ACTION_RUN);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1, intent,
                PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        am.cancel(pendingIntent);
        am.set(AlarmManager.RTC_WAKEUP, notifications.get(0).time, pendingIntent);
    }

    monitorUri(context);
}

From source file:org.dmfs.tasks.notification.NotificationUpdaterService.java

private void cancelNextDayAlarm() {
    AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
    alarmManager.cancel(mDateChangePendingIntent);
}

From source file:org.apps8os.motivator.ui.MainActivity.java

/**
 * Set the notifications for the first time. After this, the notifications are controlled from the settings activity.
 *//*from  w w w.ja  va 2  s  .c om*/
public void setNotifications() {
    // Set up notifying user to answer to the mood question
    // The time to notify the user
    Calendar notificationTime = Calendar.getInstance();
    notificationTime.set(Calendar.MINUTE, 0);
    notificationTime.set(Calendar.SECOND, 0);
    // An alarm manager for scheduling notifications
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    // Set the notification to repeat over the given time at notificationTime
    Intent notificationIntent = new Intent(this, NotificationService.class);
    notificationIntent.putExtra(NotificationService.NOTIFICATION_TYPE, NotificationService.NOTIFICATION_MOOD);
    PendingIntent pendingNotificationIntent = PendingIntent.getService(this, 0, notificationIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    alarmManager.cancel(pendingNotificationIntent);
    if (notificationTime.get(Calendar.HOUR_OF_DAY) >= 10) {
        notificationTime.add(Calendar.DATE, 1);
    }
    notificationTime.set(Calendar.HOUR_OF_DAY, 10);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, notificationTime.getTimeInMillis(),
            AlarmManager.INTERVAL_DAY, pendingNotificationIntent);

    /** Commented out for now, setting different times for the notifications.
    if (mTimeToNotify == getString(R.string.in_the_morning_value)) {
       if (notificationTime.get(Calendar.HOUR_OF_DAY) >= 10) {
    notificationTime.add(Calendar.DATE, 1);
       }
       notificationTime.set(Calendar.HOUR_OF_DAY, 10);
       alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, notificationTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingNotificationIntent);
    } else if (mTimeToNotify == getString(R.string.morning_and_evening_value)) {
       if (notificationTime.get(Calendar.HOUR_OF_DAY) >= 10 && notificationTime.get(Calendar.HOUR_OF_DAY) < 22) {
    notificationTime.set(Calendar.HOUR_OF_DAY, 22);
       } else if (notificationTime.get(Calendar.HOUR_OF_DAY) < 10) {
    notificationTime.set(Calendar.HOUR_OF_DAY, 10);
       } else {
    notificationTime.add(Calendar.DATE, 1);
    notificationTime.set(Calendar.HOUR_OF_DAY, 10);
       }
       alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, notificationTime.getTimeInMillis(), AlarmManager.INTERVAL_HALF_DAY, pendingNotificationIntent);
    } else if (mTimeToNotify == getString(R.string.every_hour_value)) {
       notificationTime.add(Calendar.HOUR_OF_DAY, 1);
       alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, notificationTime.getTimeInMillis(), AlarmManager.INTERVAL_HOUR, pendingNotificationIntent);
    }
    **/
}

From source file:org.smssecure.smssecure.notifications.MessageNotifier.java

public static void clearReminder(Context context) {
    Intent alarmIntent = new Intent(ReminderReceiver.REMINDER_ACTION);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.cancel(pendingIntent);/*from   w w w . ja va2 s  . c o m*/
}

From source file:org.kaoriha.phonegap.plugins.releasenotification.Notifier.java

private void schedule(long after) {
    Intent intent = new Intent(ctx, Receiver.class);
    PendingIntent pi = PendingIntent.getBroadcast(ctx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager am = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
    am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + after, pi);
}

From source file:org.zirco.ui.activities.MainActivity.java

/**
 * Restart the application.//w w w  . j  av  a 2s  . c  o m
 */
public void restartApplication() {
    PendingIntent intent = PendingIntent.getActivity(this.getBaseContext(), 0, new Intent(getIntent()),
            getIntent().getFlags());
    AlarmManager mgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 2000, intent);
    System.exit(2);
}

From source file:com.google.android.apps.paco.NotificationCreator.java

private void createAlarmForSnooze(Context context, NotificationHolder notificationHolder) {
    DateTime alarmTime = new DateTime(notificationHolder.getAlarmTime());
    Experiment experiment = experimentProviderUtil.getExperiment(notificationHolder.getExperimentId());
    Integer snoozeTime = experiment.getSignalingMechanisms().get(0).getSnoozeTime();
    int snoozeMinutes = snoozeTime / MILLIS_IN_MINUTE;
    DateTime timeoutMinutes = new DateTime(alarmTime).plusMinutes(snoozeMinutes);
    long snoozeDurationInMillis = timeoutMinutes.getMillis();

    Log.i(PacoConstants.TAG,//from   w  ww.j  a  v a 2  s. co  m
            "Creating snooze alarm to resound notification for holder: " + notificationHolder.getId()
                    + ". experiment = " + notificationHolder.getExperimentId() + ". alarmtime = "
                    + new DateTime(alarmTime).toString() + " waking up from snooze in " + timeoutMinutes
                    + " minutes");

    Intent ultimateIntent = new Intent(context, AlarmReceiver.class);
    Uri uri = Uri.withAppendedPath(NotificationHolderColumns.CONTENT_URI,
            notificationHolder.getId().toString());
    ultimateIntent.setData(uri);
    ultimateIntent.putExtra(NOTIFICATION_ID, notificationHolder.getId().longValue());
    ultimateIntent.putExtra(SNOOZE_REPEATER_EXTRA_KEY, true);

    PendingIntent intent = PendingIntent.getBroadcast(context.getApplicationContext(), 3, ultimateIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.cancel(intent);
    alarmManager.set(AlarmManager.RTC_WAKEUP, snoozeDurationInMillis, intent);
}

From source file:at.vcity.androidimsocket.services.IMService.java

@Override
public void onDestroy() {
    try {//from ww w.  j  ava2 s . c  om
        unregisterReceiver(alarmReceiver);
    } catch (IllegalArgumentException e) {
    }

    AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(this, IMService.class);
    PendingIntent pintent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    alarm.cancel(pintent);

    Log.i("IMService is being destroyed", "...");
    //   super.onDestroy();
}

From source file:com.av.remusic.service.MediaService.java

@Override
public void onCreate() {
    if (D)//w  ww. j a va  2  s .c o  m
        Log.d(TAG, "Creating service");
    super.onCreate();
    mGetUrlThread.start();
    mLrcThread.start();
    mProxy = new MediaPlayerProxy(this);
    mProxy.init();
    mProxy.start();

    mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    // gets a pointer to the playback state store
    mPlaybackStateStore = MusicPlaybackState.getInstance(this);
    mSongPlayCount = SongPlayCount.getInstance(this);
    mRecentStore = RecentStore.getInstance(this);

    mHandlerThread = new HandlerThread("MusicPlayerHandler", android.os.Process.THREAD_PRIORITY_BACKGROUND);
    mHandlerThread.start();

    mPlayerHandler = new MusicPlayerHandler(this, mHandlerThread.getLooper());

    mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    mMediaButtonReceiverComponent = new ComponentName(getPackageName(),
            MediaButtonIntentReceiver.class.getName());
    mAudioManager.registerMediaButtonEventReceiver(mMediaButtonReceiverComponent);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        setUpMediaSession();
    }

    mPreferences = getSharedPreferences("Service", 0);
    mCardId = getCardId();

    registerExternalStorageListener();

    mPlayer = new MultiPlayer(this);
    mPlayer.setHandler(mPlayerHandler);

    // Initialize the intent filter and each action
    final IntentFilter filter = new IntentFilter();
    filter.addAction(SERVICECMD);
    filter.addAction(TOGGLEPAUSE_ACTION);
    filter.addAction(PAUSE_ACTION);
    filter.addAction(STOP_ACTION);
    filter.addAction(NEXT_ACTION);
    filter.addAction(PREVIOUS_ACTION);
    filter.addAction(PREVIOUS_FORCE_ACTION);
    filter.addAction(REPEAT_ACTION);
    filter.addAction(SHUFFLE_ACTION);
    filter.addAction(TRY_GET_TRACKINFO);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    filter.addAction(LOCK_SCREEN);
    filter.addAction(SEND_PROGRESS);
    filter.addAction(SETQUEUE);
    // Attach the broadcast listener
    registerReceiver(mIntentReceiver, filter);

    mMediaStoreObserver = new MediaStoreObserver(mPlayerHandler);
    getContentResolver().registerContentObserver(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, true,
            mMediaStoreObserver);
    getContentResolver().registerContentObserver(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, true,
            mMediaStoreObserver);

    // Initialize the wake lock
    final PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
    mWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
    mWakeLock.setReferenceCounted(false);

    final Intent shutdownIntent = new Intent(this, MediaService.class);
    shutdownIntent.setAction(SHUTDOWN);

    mAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    mShutdownIntent = PendingIntent.getService(this, 0, shutdownIntent, 0);

    scheduleDelayedShutdown();

    reloadQueueAfterPermissionCheck();
    notifyChange(QUEUE_CHANGED);
    notifyChange(META_CHANGED);
}