Example usage for android.app AlarmManager set

List of usage examples for android.app AlarmManager set

Introduction

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

Prototype

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

Source Link

Document

Schedule an alarm.

Usage

From source file:cc.softwarefactory.lokki.android.services.LocationService.java

private void setTemporalTimer() {
    AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE);
    Intent alarmIntent = new Intent(this, LocationService.class);
    alarmIntent.putExtra(ALARM_TIMER, 1);
    PendingIntent alarmCallback = PendingIntent.getService(this, 0, alarmIntent, 0);
    alarm.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + INTERVAL_1_MIN,
            alarmCallback);//from  w w  w . j ava 2s . c  o  m
    Log.e(TAG, "Time created.");
}

From source file:de.geeksfactory.opacclient.reminder.ReminderCheckService.java

@Override
public int onStartCommand(Intent intent, int flags, int startid) {
    if (ACTION_SNOOZE.equals(intent.getAction())) {
        Intent i = new Intent(ReminderCheckService.this, ReminderAlarmReceiver.class);
        PendingIntent sender = PendingIntent.getBroadcast(ReminderCheckService.this,
                OpacClient.BROADCAST_REMINDER, i, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        Log.i("ReminderCheckService", "Opac App Service: Quick repeat");
        // Run again in 1 day
        am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (1000 * 3600 * 24), sender);

        NotificationManager mNotificationManager = (NotificationManager) getSystemService(
                Context.NOTIFICATION_SERVICE);
        mNotificationManager.cancel(OpacClient.NOTIF_ID);
    } else {//from   w ww . java  2 s.  c om
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(ReminderCheckService.this);
        notification_on = sp.getBoolean("notification_service", false);
        long waittime = (1000 * 3600 * 5);
        boolean executed = false;

        ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
        if (networkInfo != null) {
            if (!sp.getBoolean("notification_service_wifionly", false)
                    || networkInfo.getType() == ConnectivityManager.TYPE_WIFI
                    || networkInfo.getType() == ConnectivityManager.TYPE_ETHERNET) {
                executed = true;
                new CheckTask().execute();
            } else {
                waittime = (1000 * 1800);
            }
        } else {
            waittime = (1000 * 1800);
        }

        if (!notification_on) {
            waittime = (1000 * 3600 * 12);
        }

        Intent i = new Intent(ReminderCheckService.this, ReminderAlarmReceiver.class);
        PendingIntent sender = PendingIntent.getBroadcast(ReminderCheckService.this,
                OpacClient.BROADCAST_REMINDER, i, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + waittime, sender);

        if (!executed) {
            stopSelf();
        }
    }

    return START_NOT_STICKY;
}

From source file:com.dogar.geodesic.map.MainActivity.java

private void restartApp() {
    Intent mStartActivity = new Intent(this, MainActivity.class);
    int mPendingIntentId = 123456;
    PendingIntent mPendingIntent = PendingIntent.getActivity(this, mPendingIntentId, mStartActivity,
            PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager mgr = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
    mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
    System.exit(0);// ww w. j a va  2 s .co  m
}

From source file:de.appplant.cordova.plugin.notification.NotificationWrapper.java

/**
 * Schedule new notification//  ww w  .j a  v a 2  s .com
 */
public void schedule(Options options) {
    long triggerTime = options.getDate();

    persist(options.getId(), options.getJSONObject());

    //Intent is called when the Notification gets fired
    Intent intent = new Intent(context, receiver).setAction("" + options.getId()).putExtra(OPTIONS,
            options.getJSONObject().toString());

    AlarmManager am = getAlarmManager();
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

    am.set(AlarmManager.RTC_WAKEUP, triggerTime, pi);
}

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

private void createAlarmToCancelNotificationAtTimeout(Context context, NotificationHolder notificationHolder) {
    DateTime alarmTime = new DateTime(notificationHolder.getAlarmTime());
    int timeoutMinutes = (int) (notificationHolder.getTimeoutMillis() / MILLIS_IN_MINUTE);
    DateTime timeoutTime = new DateTime(alarmTime).plusMinutes(timeoutMinutes);
    long elapsedDurationInMillis = timeoutTime.getMillis();

    Log.i(PacoConstants.TAG,//from   w w  w .jav  a2s  .  c o  m
            "Creating cancel alarm to timeout notification for holder: " + notificationHolder.getId()
                    + ". experiment = " + notificationHolder.getExperimentId() + ". alarmtime = "
                    + new DateTime(alarmTime).toString() + " timing out 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());

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

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

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 www.j av  a 2  s  . c  om*/
            "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:com.daiv.android.twitter.services.TrimDataService.java

public void setNextTrim(Context context) {
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    long now = new Date().getTime();
    long alarm = now + AlarmManager.INTERVAL_DAY;

    Log.v("alarm_date", "auto trim " + new Date(alarm).toString());

    PendingIntent pendingIntent = PendingIntent.getService(context, TRIM_ID,
            new Intent(context, TrimDataService.class), 0);

    am.set(AlarmManager.RTC_WAKEUP, alarm, pendingIntent);
}

From source file:shetye.prathamesh.notifyme.Utilities.java

private void setZoneInTimer(Context context, Notif note) {
    Intent intent = new Intent(context, RecieveAndNotify.class);
    intent.setData(Uri.parse(Integer.toString(note.get_id())));
    intent.setAction(NOTIF_SERVICE_ACTION);
    intent.putExtra(NOTIF_EXTRA_TITLE_KEY, note.getNotification_title());
    intent.putExtra(NOTIF_EXTRA_KEY, note.getNotification_content());
    intent.putExtra(NOTIF_EXTRA_ID_KEY, note.get_id());
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    am.set(AlarmManager.RTC_WAKEUP, note.getNotification_when(), PendingIntent.getBroadcast(context, 0, intent,
            PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_CANCEL_CURRENT));
}

From source file:at.andreasrohner.spartantimelapserec.BackgroundService.java

@SuppressWarnings("deprecation")
@Override/*from  www.  ja v a  2  s  .c  o m*/
public void onCreate() {
    created = true;

    settings = new RecSettings();
    settings.load(getApplicationContext(),
            PreferenceManager.getDefaultSharedPreferences(getApplicationContext()));

    createNotification(NOTIFICATION_ID);

    if (settings.isSchedRecEnabled() && settings.getSchedRecTime() > System.currentTimeMillis() + 10000) {
        AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(getApplicationContext(), ScheduleReceiver.class);
        PendingIntent alarmIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);

        alarmMgr.set(AlarmManager.RTC_WAKEUP, settings.getSchedRecTime(), alarmIntent);

        stopSelf();
        return;
    }

    // Create new SurfaceView, set its size to 1x1, move
    // it to the top left corner and set this service as a callback
    WindowManager winMgr = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
    surfaceView = new SurfaceView(this);
    // deprecated setting, but required on Android versions prior to 3.0
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB)
        surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    surfaceView.getHolder().addCallback(this);

    LayoutParams layoutParams = new WindowManager.LayoutParams(1, 1,
            WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
            PixelFormat.TRANSLUCENT);
    layoutParams.gravity = Gravity.LEFT | Gravity.TOP;
    winMgr.addView(surfaceView, layoutParams);

    PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
    wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
    wakeLock.setReferenceCounted(false);
    wakeLock.acquire();
}

From source file:com.futureplatforms.kirin.extensions.localnotifications.LocalNotificationsBackend.java

protected boolean scheduleSingleNotification(AlarmManager am, JSONObject obj) {
    PendingIntent pendingIntent = createPendingIntentForSchedule(obj.optString("id"));
    long time = obj.optLong("timeMillisSince1970", -1);
    if (time < 0) {
        return false;
    }/*from   w ww.j ava2s. c  o  m*/
    am.set(AlarmManager.RTC_WAKEUP, time, pendingIntent);
    return true;
}