Example usage for android.app AlarmManager setExact

List of usage examples for android.app AlarmManager setExact

Introduction

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

Prototype

public void setExact(@AlarmType int type, long triggerAtMillis, PendingIntent operation) 

Source Link

Document

Schedule an alarm to be delivered precisely at the stated time.

Usage

From source file:com.meiste.greg.ptw.WidgetProvider.java

@SuppressLint("NewApi")
private void setAlarm(final Context context) {
    /* No point setting alarm if no widgets */
    if (getInstalledWidgets(context).length == 0)
        return;//from   www.  ja v a 2 s  .  c  o m

    final long now = System.currentTimeMillis();
    long next = UPDATE_INTERVAL - (now % UPDATE_INTERVAL) - UPDATE_FUDGE;
    if (next <= 0) {
        next += UPDATE_INTERVAL;
    }

    final AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        am.setExact(AlarmManager.RTC, now + next, getAlarmIntent(context));
    } else {
        am.set(AlarmManager.RTC, now + next, getAlarmIntent(context));
    }
}

From source file:com.ayuget.redface.job.PrivateMessagesService.java

@TargetApi(android.os.Build.VERSION_CODES.KITKAT)
@Override//from   www. ja  v a  2 s  .com
protected void onHandleIntent(Intent intent) {
    Log.d(LOG_TAG, "Handling intent");

    if (!settings.arePrivateMessagesNoticationsEnabled()) {
        return;
    }

    final NotificationManagerCompat notificationManager = NotificationManagerCompat
            .from(getApplicationContext());

    for (User redfaceUser : userManager.getRealUsers()) {
        subscriptions.add(mdService.getNewPrivateMessages(redfaceUser)
                .subscribe(new EndlessObserver<List<PrivateMessage>>() {
                    @Override
                    public void onNext(List<PrivateMessage> privateMessages) {
                        for (PrivateMessage privateMessage : privateMessages) {
                            // Prepare intent to deal with clicks
                            Intent resultIntent = new Intent(PrivateMessagesService.this,
                                    PrivateMessagesActivity.class);
                            resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            resultIntent.putExtra(UIConstants.ARG_SELECTED_PM, privateMessage);
                            PendingIntent resultPendingIntent = PendingIntent.getActivity(
                                    getApplicationContext(), 0, resultIntent,
                                    PendingIntent.FLAG_UPDATE_CURRENT);

                            NotificationCompat.Builder builder = new NotificationCompat.Builder(
                                    getApplicationContext()).setSmallIcon(R.drawable.ic_action_emo_wonder)
                                            .setColor(getResources().getColor(R.color.theme_primary))
                                            .setContentTitle(privateMessage.getRecipient())
                                            .setContentText(privateMessage.getSubject())
                                            .setContentIntent(resultPendingIntent).setAutoCancel(true);

                            builder.setVibrate(VIBRATION_PATTERN);

                            notificationManager.notify((int) privateMessage.getId(), builder.build());
                        }
                    }
                }));
    }

    // Setup next alarm
    long wakeUpTime = System.currentTimeMillis()
            + settings.getPrivateMessagesPollingFrequency() * DateUtils.MINUTE_IN_MILLIS;
    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(this, PrivateMessagesService.class);
    PendingIntent pi = PendingIntent.getService(this, 0, i, 0);
    Log.d(LOG_TAG, "Going to sleep, setting wake-up alarm to: " + wakeUpTime);
    if (AndroidUtils.isKitKatOrHigher()) {
        am.setExact(AlarmManager.RTC_WAKEUP, wakeUpTime, pi);
    } else {
        am.set(AlarmManager.RTC_WAKEUP, wakeUpTime, pi);
    }

}

From source file:com.embeddedlog.LightUpDroid.timer.TimerReceiver.java

private void showCollapsedNotificationWithNext(final Context context, String title, String text,
        Long nextBroadcastTime) {
    Intent activityIntent = new Intent(context, DeskClock.class);
    activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    activityIntent.putExtra(DeskClock.SELECT_TAB_INTENT_EXTRA, DeskClock.TIMER_TAB_INDEX);
    PendingIntent pendingActivityIntent = PendingIntent.getActivity(context, 0, activityIntent,
            PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT);
    showCollapsedNotification(context, title, text, Notification.PRIORITY_HIGH, pendingActivityIntent,
            IN_USE_NOTIFICATION_ID, false);

    if (nextBroadcastTime == null) {
        return;//from   w ww  .ja va  2 s  . c o m
    }
    Intent nextBroadcast = new Intent();
    nextBroadcast.setAction(Timers.NOTIF_IN_USE_SHOW);
    PendingIntent pendingNextBroadcast = PendingIntent.getBroadcast(context, 0, nextBroadcast, 0);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    if (Utils.isKitKatOrLater()) {
        alarmManager.setExact(AlarmManager.ELAPSED_REALTIME, nextBroadcastTime, pendingNextBroadcast);
    } else {
        alarmManager.set(AlarmManager.ELAPSED_REALTIME, nextBroadcastTime, pendingNextBroadcast);
    }
}

From source file:com.android.deskclock.timer.TimerReceiver.java

private void showCollapsedNotificationWithNext(final Context context, String title, String text,
        Long nextBroadcastTime) {
    LogUtils.d(TAG, "showCollapsedNotificationWithNext nextBroadcastTime: %d", nextBroadcastTime);
    Intent activityIntent = new Intent(context, DeskClock.class);
    activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    activityIntent.putExtra(DeskClock.SELECT_TAB_INTENT_EXTRA, DeskClock.TIMER_TAB_INDEX);
    PendingIntent pendingActivityIntent = PendingIntent.getActivity(context, 0, activityIntent,
            PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT);
    showCollapsedNotification(context, title, text, NotificationCompat.PRIORITY_HIGH, pendingActivityIntent,
            IN_USE_NOTIFICATION_ID, false);

    if (nextBroadcastTime == null) {
        return;/*w  ww  .  j  a v  a 2  s  .  c o m*/
    }
    Intent nextBroadcast = new Intent();
    nextBroadcast.setAction(Timers.NOTIF_IN_USE_SHOW);
    PendingIntent pendingNextBroadcast = PendingIntent.getBroadcast(context, 0, nextBroadcast, 0);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    if (Utils.isKitKatOrLater()) {
        alarmManager.setExact(AlarmManager.ELAPSED_REALTIME, nextBroadcastTime, pendingNextBroadcast);
    } else {
        alarmManager.set(AlarmManager.ELAPSED_REALTIME, nextBroadcastTime, pendingNextBroadcast);
    }
}

From source file:com.hodor.company.areminder.ui.MainActivity.java

private void registerAlarmManager(long duration) {
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(ACTION_SHOW_ALARM, null, this, TimerService.class);
    intent.putExtra("category", category.ordinal());
    PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    long time = System.currentTimeMillis() + duration;
    alarmManager.setExact(AlarmManager.RTC_WAKEUP, time, pendingIntent);
}

From source file:com.example.android.wearable.timer.SetTimerActivity.java

private void registerWithAlarmManager(long duration) {
    // Get the alarm manager.
    AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    // Create intent that gets fired when timer expires.
    Intent intent = new Intent(Constants.ACTION_SHOW_ALARM, null, this, TimerNotificationService.class);
    PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    // Calculate the time when it expires.
    long wakeupTime = System.currentTimeMillis() + duration;

    // Schedule an alarm.
    alarm.setExact(AlarmManager.RTC_WAKEUP, wakeupTime, pendingIntent);
}

From source file:com.embeddedlog.LightUpDroid.timer.TimerReceiver.java

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);/*from w w w.j a  v a  2 s . c  o  m*/
    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) {
        if (Utils.isKitKatOrLater()) {
            mngr.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, nextTimesup, p);
        } else {
            mngr.set(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");
        }
    }
}

From source file:se.oort.clockify.timer.TimerReceiver.java

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);/* www  .ja va  2  s  .  c o  m*/
    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) {
        if (Utils.isKitKatOrLater()) {
            mngr.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, nextTimesup, p);
        } else {
            mngr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, nextTimesup, p);
        }
        if (Timers.LOGGING) {
            Log.d(LOG_TAG, "Setting times up to " + nextTimesup);
        }
    } else {
        mngr.cancel(p);
        if (Timers.LOGGING) {
            Log.v(LOG_TAG, "no next times up");
        }
    }
}

From source file:com.android.deskclock.timer.TimerReceiver.java

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);//from  w w w  .j  a v a 2  s  . c o  m
    intent.setClass(context, TimerReceiver.class);
    // Time-critical, should be foreground
    intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
    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) {
        if (Utils.isKitKatOrLater()) {
            mngr.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, nextTimesup, p);
        } else {
            mngr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, nextTimesup, p);
        }
        if (Timers.LOGGING) {
            Log.d(TAG, "Setting times up to " + nextTimesup);
        }
    } else {
        // if no timer is found Pending Intents should be canceled
        // to keep the internal state consistent with the UI
        mngr.cancel(p);
        p.cancel();
        if (Timers.LOGGING) {
            Log.v(TAG, "no next times up");
        }
    }
}

From source file:com.tortel.deploytrack.service.NotificationService.java

@SuppressLint("NewApi")
private void showNotification() {
    // If there isnt an ID saved, shut down the service
    if (deploymentId == -1) {
        stopSelf();//from ww  w .  j ava  2 s .  co m
        return;
    }
    if (DEBUG) {
        Toast.makeText(this, "NotificationService loading notification", Toast.LENGTH_SHORT).show();
    }

    // Load the Deployment object
    Deployment deployment = DatabaseManager.getInstance(this).getDeployment(deploymentId);
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

    RemoteViews view = new RemoteViews(getPackageName(), R.layout.notification);
    view.setImageViewBitmap(R.id.notification_pie, WidgetProvider.getChartBitmap(deployment, SIZE));
    view.setTextViewText(R.id.notification_title, deployment.getName());

    view.setTextViewText(R.id.notification_main, getResources().getString(R.string.small_notification,
            deployment.getPercentage(), deployment.getCompleted(), deployment.getLength()));

    if (prefs.getBoolean(Prefs.KEY_HIDE_DATE, false)) {
        view.setViewVisibility(R.id.notification_daterange, View.GONE);
    } else {
        view.setTextViewText(R.id.notification_daterange, getResources().getString(R.string.date_range,
                deployment.getFormattedStart(), deployment.getFormattedEnd()));
    }

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setContentTitle(deployment.getName());
    builder.setContentText(getResources().getString(R.string.small_notification, deployment.getPercentage(),
            deployment.getCompleted(), deployment.getLength()));
    builder.setOngoing(true);

    // Hide the time, its persistent
    builder.setWhen(0);

    builder.setSmallIcon(R.drawable.ic_notification);
    builder.setPriority(Integer.MAX_VALUE);

    Notification notification = builder.build();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        notification.bigContentView = view;
    }

    notificationManager.notify(NOTIFICATION_ID, notification);

    //Schedule an update at midnight
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    DateTime now = new DateTime();
    DateTime tomorrow = new DateTime(now.plusDays(1)).withTimeAtStartOfDay();

    PendingIntent pending = PendingIntent.getBroadcast(getBaseContext(), 0, new Intent(UPDATE_INTENT),
            PendingIntent.FLAG_UPDATE_CURRENT);

    //Adding 100msec to make sure its triggered after midnight
    Log.d("Scheduling notification update for " + tomorrow.getMillis() + 100);

    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR2) {
        alarmManager.setExact(AlarmManager.RTC, tomorrow.getMillis() + 100, pending);
    } else {
        alarmManager.set(AlarmManager.RTC, tomorrow.getMillis() + 100, pending);
    }
}