Example usage for android.app AlarmManager RTC_WAKEUP

List of usage examples for android.app AlarmManager RTC_WAKEUP

Introduction

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

Prototype

int RTC_WAKEUP

To view the source code for android.app AlarmManager RTC_WAKEUP.

Click Source Link

Document

Alarm time in System#currentTimeMillis System.currentTimeMillis() (wall clock time in UTC), which will wake up the device when it goes off.

Usage

From source file:com.cpd.receivers.LibraryRenewAlarmBroadcastReceiver.java

public void setAlarm(Context context) {
    Log.d(TAG, "setAlarm() called with: " + "context = [" + context + "]");
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 12);
    calendar.set(Calendar.MINUTE, 1);
    calendar.set(Calendar.SECOND, 0);

    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, LibraryRenewAlarmBroadcastReceiver.class);

    PendingIntent pi = PendingIntent.getBroadcast(context, 1, intent, 0);

    am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
            AlarmManager.INTERVAL_DAY / DAILY_RETRIES, pi);
}

From source file:es.uva.tel.gte.cardiomanager.main.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.slide_main);

    // Comprobamos la alarma que nos indica si se ha cerrado o no la aplicacin
    DBAdapter dbAdapter = new DBAdapter(getApplicationContext());
    dbAdapter.open();//from  www. j a v  a  2  s .c  o  m

    boolean alarmUp = isMyServiceRunning(AppService.class);

    if (alarmUp) {
        // No se ha cerrado la aplicacin
        Log.println(Log.ASSERT, "Main Activity", "La alarma estaba activa");
    } else {
        Log.println(Log.ASSERT, "Main Activity", "Activamos alarma");
        // Se ha cerrado la aplicacion
        // Volvemos a poner la alarma
        Intent intent = new Intent(getApplicationContext(), CheckAlarmsService.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 123456789, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.add(Calendar.YEAR, 1); // Alarma a 1 ao, tiempo suficiente 
        Log.println(Log.ASSERT, "Alarm Created", "Alarm Created");
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60,
                pendingIntent);
        // Comprobamos ahora cuantos medicamentos no se han tomado
        startService(intent);

    }

    // Instantiate a ViewPager and a PagerAdapter.
    mPager = (ViewPager) findViewById(R.id.pager);
    mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
    mPager.setAdapter(mPagerAdapter);

    fManager = getSupportFragmentManager();

    Cursor cursor = dbAdapter.fetchAllRows(DBAdapter.TABLE_NAME_POLITICS_ACCEPTED);
    if (cursor.moveToFirst()) {
        if (cursor.getInt(1) == 1 && cursor.getInt(2) == 1) {
            // Accepted
        } else {
            // Mostrar dilogo
            PoliticDialog newFragment = new PoliticDialog();
            newFragment.show(fManager, "Tag");
        }
    } else {
        // Mostrar dilogo
        PoliticDialog newFragment = new PoliticDialog();
        newFragment.show(fManager, "Tag");
    }
    dbAdapter.close();

}

From source file:felixwiemuth.lincal.NotificationService.java

@Override
protected void onHandleIntent(Intent intent) {
    calendars = Calendars.getInstance(this);
    Calendar now = Calendar.getInstance();
    Calendar nextAlarm = null;/*from   w  ww  .  j  a v  a2 s.c o  m*/
    for (int i = 0; i < calendars.getCalendarCount(); i++) {
        LinCalConfig config = calendars.getConfigByPos(i);
        if (config.isNotificationsEnabled()) { // only load calendar if notifications are enabled
            LinCal cal = calendars.getCalendarByPos(this, i);
            if (cal != null) { // if the calendar could not be loaded, skip it (this will also skip scheduling of next notifications for this calendar)
                Calendar nextTime = processCalendar(cal, config, now);
                if (nextAlarm == null || (nextTime != null && nextTime.before(nextAlarm))) {
                    nextAlarm = nextTime;
                }
            }
        }
    }
    calendars.save(this);
    // Schedule next processing if there are further entries
    if (nextAlarm != null) {
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        Intent processIntent = new Intent(this, NotificationService.class);
        PendingIntent alarmIntent = PendingIntent.getService(this, 0, processIntent, 0);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            alarmManager.setExact(AlarmManager.RTC_WAKEUP, nextAlarm.getTimeInMillis(), alarmIntent);
        } else {
            alarmManager.set(AlarmManager.RTC_WAKEUP, nextAlarm.getTimeInMillis(), alarmIntent);
        }
    }
    stopSelf();
}

From source file:edu.rit.csh.androidwebnews.RecentActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    newsgroupListMenu = new NewsgroupListMenu(this);

    newsgroupListMenu.checkEnabled();//from   ww  w  .  j a  v  a2 s .  c  o  m

    dialog = new InvalidApiKeyDialog(this);
    connectionDialog = new ConnectionExceptionDialog(this);
    ftd = new FirstTimeDialog(this);

    setContentView(R.layout.activity_recent);

    rf = (RecentFragment) getSupportFragmentManager().findFragmentById(R.id.recent_fragment);

    if (!sharedPref.getBoolean("first_time", true)) {
        hc.getNewest(true);
        if (!sharedPref.getString("newsgroups_json_string", "").equals("")) {
            newsgroupListMenu
                    .update(hc.getNewsGroupFromString(sharedPref.getString("newsgroups_json_string", "")));
            hc.startUnreadCountTask();
        } else {
            hc.getNewsGroups();
        }

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

        // if the run service is selected, an alarm is started to repeat over given time
        if (sharedPref.getBoolean("run_service", false)) {
            String timeString = sharedPref.getString("time_between_checks", "15");
            int time = 15;
            if (!timeString.equals("")) {
                time = Integer.valueOf(timeString);
            }
            alarm.cancel(pintent);
            alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), time * 60000, pintent);
        } else {
            alarm.cancel(pintent);
        }
    } else {

        ftd.show();
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putBoolean("first_time", false);
        editor.commit();

    }
    setTitle("Recent Posts");
}

From source file:com.bluros.music.widgets.SleepModeDialog.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    context = getActivity().getBaseContext();
    Intent action = new Intent(MusicService.SLEEP_MODE_STOP_ACTION);
    ComponentName serviceName = new ComponentName(context, MusicService.class);
    action.setComponent(serviceName);/*ww  w  .j  a v  a2  s  . c o m*/
    pendingIntent = PendingIntent.getService(context, 4, action, 0);
    alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.sleep_mode_time_selector, null);
    int minutes = Integer.valueOf(getString(R.string.default_interval));
    final TextView tvPopUpTime = (TextView) view.findViewById(R.id.pop_up_time);
    tvPopUpTime.setText(String.valueOf(minutes));
    final SeekBar sBar = (SeekBar) view.findViewById(R.id.seekbar);
    sBar.setProgress(minutes - 1);
    sBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

        @Override
        public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
            tvPopUpTime.setText(String.valueOf(arg1 + 1));
        }

        @Override
        public void onStartTrackingTouch(SeekBar arg0) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar arg0) {

        }
    });
    builder.setTitle(R.string.select_quit_time);
    builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int arg1) {
            long timeLeft = (sBar.getProgress() + 1) * mMill;
            alarmManager.set(AlarmManager.RTC_WAKEUP, timeLeft + System.currentTimeMillis(), pendingIntent);
            MusicPlayer.setSleepMode(true);
            Toast.makeText(context, String.format(getString(R.string.quit_warining), sBar.getProgress() + 1),
                    Toast.LENGTH_SHORT).show();
            dialog.dismiss();
        }
    });
    builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int arg1) {
            dialog.dismiss();
        }
    });
    builder.setView(view);
    return builder.create();
}

From source file:net.noio.Reminder.NotificationService.java

@Override
protected void onHandleIntent(Intent intent) {
    Log.v("NotificationService", "Starting NotificationService run");

    LinkedList<String[]> list;
    list = ReminderDB.getReminderTimes(getApplicationContext());
    long system_time = System.currentTimeMillis();

    // Alarm notification
    AlarmManager ream = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    PendingIntent repi = PendingIntent.getService(this, 6600, intent, 0);
    ream.set(AlarmManager.RTC_WAKEUP, system_time + 60000, repi);

    for (int i = 0; i < list.size(); i++) {
        String[] data = list.get(i);

        if (data[2].equals("N")) {
            long delta = Long.parseLong(data[1]) - System.currentTimeMillis();

            Log.v("NotificationService", "TIME REMAINING: " + delta);
            Log.v("NotificationService", "1 HOUR: " + this.ONE_HOUR * 1000);
            Log.v("NotificationService", "6 HOUR: " + this.SIX_HOUR * 1000);
            Log.v("NotificationService", "12 HOUR: " + this.TWELVE_HOUR * 1000);
            Log.v("NotificationService", "24 HOUR: " + this.TWENTYFOUR_HOUR * 1000);
            Log.v("NotificationService", "In reminder loop");

            Intent intent1 = new Intent(this, MainActivity.class);
            intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            if (delta < this.ONE_HOUR * 1000) {
                Log.v("NotificationService", "IN 1");
                if (ReminderDB.checkNotify(getApplicationContext(), 1, data[0])) {
                    System.out.println("IN 1b");
                    intent1.putExtra("string", data[0]);
                    intent1.putExtra("reminder", "One-hour Reminder");
                    intent1.putExtra("id",
                            Integer.parseInt(10 + ReminderDB.getID(getApplicationContext(), data[0])));
                    this.Notify(intent1);
                    ReminderDB.disableNotification(getApplicationContext(), 1, data[0]);
                }/*from   w  ww . j  a  v a 2  s .co m*/
            } else if (delta < this.SIX_HOUR * 1000 && delta > this.ONE_HOUR * 1000) {
                Log.v("NotificationService", "IN 6");
                if (ReminderDB.checkNotify(getApplicationContext(), 2, data[0])) {
                    intent1.putExtra("string", data[0]);
                    intent1.putExtra("reminder", "Six-hour Reminder");
                    intent1.putExtra("id",
                            Integer.parseInt(10 + ReminderDB.getID(getApplicationContext(), data[0])));
                    this.Notify(intent1);
                    ReminderDB.disableNotification(getApplicationContext(), 2, data[0]);
                }
            } else if (delta < this.TWELVE_HOUR * 1000 && delta > this.SIX_HOUR * 1000) {
                Log.v("NotificationService", "IN 12");
                if (ReminderDB.checkNotify(getApplicationContext(), 3, data[0])) {
                    intent1.putExtra("string", data[0]);
                    intent1.putExtra("reminder", "Twelve-hour Reminder");
                    intent1.putExtra("id",
                            Integer.parseInt(10 + ReminderDB.getID(getApplicationContext(), data[0])));
                    this.Notify(intent1);
                    ReminderDB.disableNotification(getApplicationContext(), 3, data[0]);
                }
            } else if (delta < this.TWENTYFOUR_HOUR * 1000 && delta > this.TWELVE_HOUR * 1000) {
                Log.v("NotificationService", "IN 24");
                if (ReminderDB.checkNotify(getApplicationContext(), 4, data[0])) {
                    intent1.putExtra("string", data[0]);
                    intent1.putExtra("reminder", "Twentyfour-hour Reminder");
                    intent1.putExtra("id",
                            Integer.parseInt(10 + ReminderDB.getID(getApplicationContext(), data[0])));
                    this.Notify(intent1);
                    ReminderDB.disableNotification(getApplicationContext(), 4, data[0]);
                }
            }
        } else if (data[2].equals("Y")) {
            // every minute
            if (data[3].equals("Y")) {
                Log.v("NotificationService", "Every minute reminder for " + data[0]);

                intent.putExtra("string", data[0]);
                intent.putExtra("reminder", "Every minute reminder");
                intent.putExtra("id",
                        Integer.parseInt(10 + ReminderDB.getID(getApplicationContext(), data[0])));

                this.Notify(intent);
            }

            // every hour
            if (data[4].equals("Y")) {
                // fix to be hourly
                Log.v("NotificationService", "Hourly reminder for " + data[0]);

                if (this.loopcounter == 60) {
                    this.loopcounter = 0;
                    intent.putExtra("string", data[0]);
                    intent.putExtra("reminder", "Hourly reminder");
                    intent.putExtra("id",
                            Integer.parseInt(10 + ReminderDB.getID(getApplicationContext(), data[0])));

                    this.Notify(intent);
                }

                this.loopcounter += 1;
            }
        }
    }

    Log.v("NotificationService", "Ending NotificationService run");
}

From source file:edu.umich.si.inteco.minuku.manager.MinukuNotificationManager.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d(TAG, "OnStartCommand");

    if (mNotificationManager == null) {
        mNotificationManager = (android.app.NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE);
    }/*from   ww  w. j  av a  2s  . c o  m*/

    AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarm.set(AlarmManager.RTC_WAKEUP,
            System.currentTimeMillis() + Constants.PROMPT_SERVICE_REPEAT_MILLISECONDS,
            PendingIntent.getService(this, 0, new Intent(this, MinukuNotificationManager.class), 0));

    Notification note = new Notification.Builder(getBaseContext())
            .setContentTitle(Constants.getInstance().getAppName())
            .setContentText(Constants.getInstance().getRunningAppDeclaration())
            .setSmallIcon(R.drawable.self_reflection).setAutoCancel(false).build();
    note.flags |= Notification.FLAG_NO_CLEAR;
    startForeground(42, note);

    checkRegisteredNotifications();

    return START_STICKY_COMPATIBILITY;
}

From source file:org.chromium.ChromeAlarms.java

private void create(final CordovaArgs args, final CallbackContext callbackContext) {
    try {/*w  w w. j  a v  a 2s .  co m*/
        String name = args.getString(0);
        Alarm alarm = new Alarm(name, (long) args.getDouble(1), (long) (args.optDouble(2) * 60000));
        PendingIntent alarmPendingIntent = makePendingIntentForAlarm(name, PendingIntent.FLAG_CANCEL_CURRENT);
        alarmManager.cancel(alarmPendingIntent);
        if (alarm.periodInMillis == 0) {
            alarmManager.set(AlarmManager.RTC_WAKEUP, alarm.scheduledTime, alarmPendingIntent);
        } else {
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarm.scheduledTime, alarm.periodInMillis,
                    alarmPendingIntent);
        }
        callbackContext.success();
    } catch (Exception e) {
        Log.e(LOG_TAG, "Could not create alarm", e);
        callbackContext.error("Could not create alarm");
    }
}

From source file:org.pixmob.freemobile.netstat.SyncService.java

public static void schedule(Context context, boolean enabled) {
    final Context appContext = context.getApplicationContext();
    final AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    final PendingIntent syncIntent = PendingIntent.getService(appContext, 0,
            new Intent(appContext, SyncService.class), PendingIntent.FLAG_CANCEL_CURRENT);
    am.cancel(syncIntent);/* w ww .j  a  v  a  2s .  c  o  m*/

    if (enabled) {
        // Set the sync period.
        long period = AlarmManager.INTERVAL_HOUR;
        final int syncErrors = context.getSharedPreferences(INTERNAL_SP_NAME, MODE_PRIVATE)
                .getInt(INTERNAL_SP_KEY_SYNC_ERRORS, 0);
        if (syncErrors != 0) {
            // When there was a sync error, the sync period is longer.
            period = AlarmManager.INTERVAL_HOUR * Math.min(syncErrors, MAX_SYNC_ERRORS);
        }

        // Add a random time to prevent concurrent requests for the server.
        final long fuzz = RANDOM.nextInt(1000 * 60 * 30);
        period += fuzz;

        if (DEBUG) {
            Log.d(TAG, "Scheduling synchronization: next in " + (period / 1000 / 60) + " minutes");
        }
        final long syncTime = System.currentTimeMillis() + period;
        am.set(AlarmManager.RTC_WAKEUP, syncTime, syncIntent);
    } else {
        if (DEBUG) {
            Log.d(TAG, "Synchronization schedule canceled");
        }
    }
}

From source file:com.money.manager.ex.sync.SyncSchedulerBroadcastReceiver.java

private void startHeartbeat(Context context, AlarmManager alarmManager, PendingIntent pendingIntent) {
    SyncManager sync = new SyncManager(context);
    if (!sync.isSyncEnabled())
        return;/*www .j  a va 2  s .co  m*/

    // get frequency in minutes.
    SyncPreferences preferences = new SyncPreferences(context);
    int minutes = preferences.getSyncInterval();
    // If the period is 0, do not schedule the alarm.
    if (minutes <= 0)
        return;

    MmxDate now = new MmxDate();
    int secondsInMinute = 60;

    Timber.d("Scheduling synchronisation at: %s, repeat every %s minutes", now.toString(), minutes);

    // Schedule the alarm for synchronization. Run immediately and then in the given interval.
    alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, now.getMillis(), minutes * secondsInMinute * 1000,
            pendingIntent);
}