Search in sources :

Example 96 with AlarmManager

use of android.app.AlarmManager in project android_packages_apps_OmniClock by omnirom.

the class TimerReceiver method updateNextTimesup.

// Scan all timers and find the one that will expire next.
// Tell AlarmManager to send a "Time's up" message to this receiver when this timer expires.
// If no timer exists, clear "time's up" message.
private void updateNextTimesup(Context context) {
    TimerObj t = getNextRunningTimer(mTimers, false, Utils.getTimeNow());
    long nextTimesup = (t == null) ? -1 : t.getTimesupTime();
    int timerId = (t == null) ? -1 : t.mTimerId;
    Intent intent = new Intent();
    intent.setAction(Timers.TIMES_UP);
    intent.setClass(context, TimerReceiver.class);
    if (!mTimers.isEmpty()) {
        intent.putExtra(Timers.TIMER_INTENT_EXTRA, timerId);
    }
    AlarmManager mngr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    PendingIntent p = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT);
    if (t != null) {
        mngr.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, nextTimesup, p);
        if (Timers.LOGGING) {
            Log.d(TAG, "Setting times up to " + nextTimesup);
        }
    } else {
        mngr.cancel(p);
        if (Timers.LOGGING) {
            Log.v(TAG, "no next times up");
        }
    }
}
Also used : AlarmManager(android.app.AlarmManager) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent)

Example 97 with AlarmManager

use of android.app.AlarmManager in project android_packages_apps_OmniClock by omnirom.

the class Utils method getNextAlarm.

/**
 * @return The next alarm from {@link AlarmManager}
 */
public static String getNextAlarm(Context context) {
    String timeString = null;
    final AlarmManager.AlarmClockInfo info = ((AlarmManager) context.getSystemService(Context.ALARM_SERVICE)).getNextAlarmClock();
    if (info != null) {
        final long triggerTime = info.getTriggerTime();
        final Calendar alarmTime = Calendar.getInstance();
        alarmTime.setTimeInMillis(triggerTime);
        timeString = AlarmUtils.getFormattedTime(context, alarmTime);
    }
    return timeString;
}
Also used : Calendar(java.util.Calendar) AlarmManager(android.app.AlarmManager) SpannableString(android.text.SpannableString)

Example 98 with AlarmManager

use of android.app.AlarmManager in project android_packages_apps_OmniClock by omnirom.

the class AlarmNotifications method registerNextAlarmWithAlarmManager.

public static void registerNextAlarmWithAlarmManager(Context context, AlarmInstance instance) {
    // Sets a surrogate alarm with alarm manager that provides the AlarmClockInfo for the
    // alarm that is going to fire next. The operation is constructed such that it is ignored
    // by AlarmStateManager.
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    int flags = instance == null ? PendingIntent.FLAG_NO_CREATE : 0;
    PendingIntent operation = PendingIntent.getBroadcast(context, 0, /* requestCode */
    AlarmStateManager.createIndicatorIntent(context), flags);
    if (instance != null) {
        long alarmTime = instance.getAlarmTime().getTimeInMillis();
        // Create an intent that can be used to show or edit details of the next alarm.
        PendingIntent viewIntent = PendingIntent.getActivity(context, instance.hashCode(), createViewAlarmIntent(context, instance), PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager.AlarmClockInfo info = new AlarmManager.AlarmClockInfo(alarmTime, viewIntent);
        alarmManager.setAlarmClock(info, operation);
    } else if (operation != null) {
        alarmManager.cancel(operation);
    }
}
Also used : AlarmManager(android.app.AlarmManager) PendingIntent(android.app.PendingIntent)

Example 99 with AlarmManager

use of android.app.AlarmManager in project android_packages_apps_OmniClock by omnirom.

the class AlarmStateManager method cancelScheduledInstance.

/**
 * Cancel all {@link AlarmManager} timers for instance.
 *
 * @param context application context
 * @param instance to disable all {@link AlarmManager} timers
 */
private static void cancelScheduledInstance(Context context, AlarmInstance instance) {
    LogUtils.v("Canceling instance " + instance.mId + " timers");
    // Create a PendingIntent that will match any one set for this instance
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, instance.hashCode(), createStateChangeIntent(context, ALARM_MANAGER_TAG, instance, null), PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    am.cancel(pendingIntent);
}
Also used : AlarmManager(android.app.AlarmManager) PendingIntent(android.app.PendingIntent)

Example 100 with AlarmManager

use of android.app.AlarmManager in project AmazeFileManager by TeamAmaze.

the class FTPService method onTaskRemoved.

// Restart the service if the app is closed from the recent list
@Override
public void onTaskRemoved(Intent rootIntent) {
    super.onTaskRemoved(rootIntent);
    Intent restartService = new Intent(getApplicationContext(), this.getClass());
    restartService.setPackage(getPackageName());
    PendingIntent restartServicePI = PendingIntent.getService(getApplicationContext(), 1, restartService, PendingIntent.FLAG_ONE_SHOT);
    AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 2000, restartServicePI);
}
Also used : AlarmManager(android.app.AlarmManager) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent)

Aggregations

AlarmManager (android.app.AlarmManager)471 PendingIntent (android.app.PendingIntent)349 Intent (android.content.Intent)323 Calendar (java.util.Calendar)75 SuppressLint (android.annotation.SuppressLint)24 Date (java.util.Date)24 Context (android.content.Context)22 SharedPreferences (android.content.SharedPreferences)20 SimpleDateFormat (java.text.SimpleDateFormat)18 Test (org.junit.Test)17 VisibleForTesting (com.android.internal.annotations.VisibleForTesting)13 Config (org.robolectric.annotation.Config)13 Handler (android.os.Handler)11 HashMap (java.util.HashMap)11 PowerManager (android.os.PowerManager)10 File (java.io.File)10 Map (java.util.Map)10 Activity (android.app.Activity)9 ShadowAlarmManager (org.robolectric.shadows.ShadowAlarmManager)9 TargetApi (android.annotation.TargetApi)8