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:com.njlabs.amrita.aid.news.NewsUpdateService.java

@Override
public void onDestroy() {
    // I want to restart this service again in one hour
    AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarm.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (1000 * 60 * 60 * 6),
            PendingIntent.getService(this, 0, new Intent(this, NewsUpdateService.class), 0));
}

From source file:com.group13.androidsdk.mycards.NotificationService.java

private void rescheduleNotifications() {

    List<NotificationRule> rules = new ArrayList<>();
    Collections.addAll(rules, MyCardsDBManager.getInstance(this).getAllNotificationRules());
    rules.addAll(getCalendarAsNotificationRules());

    SharedPreferences prefs = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
    long lastNotifMillis = prefs.getLong("lastNotifElapsedRealtime", -2 * FIFTEEN_MINUTES);
    SharedPreferences.Editor prefEditor = prefs.edit();

    MyCardsDBManager dbm = MyCardsDBManager.getInstance(this);
    if (!(Math.abs(lastNotifMillis - SystemClock.elapsedRealtime()) < FIFTEEN_MINUTES || dbm.getDoNotDisturb()
            || dateMatchesAnyRule(new Date(), rules)
            || dbm.getCardsForReviewBefore(new Date(), null).length == 0)) {
        lastNotifMillis = SystemClock.elapsedRealtime();
        prefEditor.putLong("lastNotifElapsedRealtime", lastNotifMillis);

        sendNotification();/*w w w  .j av a 2  s .co  m*/
    }

    Intent intent = new Intent(this, NotificationService.class);
    PendingIntent pi = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + FIFTEEN_MINUTES, pi);

    prefEditor.putLong("lastRunElapsedRealtime", SystemClock.elapsedRealtime());
    prefEditor.apply();
}

From source file:me.futuretechnology.blops.ui.HomeActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // startService(new Intent(getApplication(), CleanupService.class));

    // if (sharedPrefs.getBoolean(Keys.FIRST_RUN, true))
    // {/*w  w w . j a va 2s . c o  m*/
    // Editor editor = sharedPrefs.edit();
    // editor.putBoolean(Keys.FIRST_RUN, false);
    // editor.apply();

    // schedule cleanup service
    // FIXME temp fix
    Time time = new Time();
    time.setToNow();
    // time.hour = 3;
    // time.minute = 0;
    // time.second = 0;
    // ++time.monthDay;
    // time.normalize(true);

    Intent intent = new Intent(getApplication(), OnAlarmReceiver.class);
    intent.setAction(OnAlarmReceiver.ACTION_CLEANUP);

    AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    am.set(AlarmManager.RTC_WAKEUP, time.toMillis(true),
            PendingIntent.getBroadcast(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT));
    // }

    EventBus.getDefault().register(this);
}

From source file:com.sean.takeastand.alarmprocess.ScheduledRepeatingAlarm.java

private void setAlarm(long triggerTime) {
    Calendar nextAlarmTime = Calendar.getInstance();
    nextAlarmTime.add(Calendar.MINUTE, mCurrentAlarmSchedule.getFrequency());
    if ((mCurrentAlarmSchedule.getEndTime()).before(nextAlarmTime)) {
        Utils.setRunningScheduledAlarm(mContext, -1);
        Log.i(TAG, mContext.getString(R.string.alarm_day_over));
        Toast.makeText(mContext, "Scheduled reminders over", Toast.LENGTH_LONG).show();
        Utils.setImageStatus(mContext, Constants.NO_ALARM_RUNNING);
        cancelAlarm();// w  ww.ja v  a2  s.  co m
        endSessionAnalytics();
    } else {
        PendingIntent pendingIntent = createPendingIntent(mContext, mCurrentAlarmSchedule);
        AlarmManager am = ((AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE));
        am.set(AlarmManager.ELAPSED_REALTIME, triggerTime, pendingIntent);
        Utils.setRunningScheduledAlarm(mContext, mCurrentAlarmSchedule.getUID());
    }
}

From source file:org.zywx.wbpalmstar.engine.universalex.EUExWidgetOne.java

public void reStartApp() {
    Intent oldData = ((Activity) mContext).getIntent();
    Intent intent = new Intent(oldData);
    int flag = ((Activity) mContext).getIntent().getFlags();
    PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, flag);
    AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
    long delay = System.currentTimeMillis() + 1000L;
    alarmManager.set(AlarmManager.RTC, delay, pendingIntent);
    ((EBrowserActivity) mContext).exitApp(false);
    ;// ww w.  j  av  a  2 s .  c o m
}

From source file:com.google.android.apps.muzei.sync.TaskQueueService.java

private void scheduleRetryArtworkDownload() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        JobScheduler jobScheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
        jobScheduler.schedule(new JobInfo.Builder(LOAD_ARTWORK_JOB_ID,
                new ComponentName(this, DownloadArtworkJobService.class))
                        .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY).build());
    } else {//w  w w .  ja  v  a  2 s. com
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
        int reloadAttempt = sp.getInt(PREF_ARTWORK_DOWNLOAD_ATTEMPT, 0);
        sp.edit().putInt(PREF_ARTWORK_DOWNLOAD_ATTEMPT, reloadAttempt + 1).commit();
        AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        long retryTimeMillis = SystemClock.elapsedRealtime() + (1 << reloadAttempt) * 2000;
        am.set(AlarmManager.ELAPSED_REALTIME, retryTimeMillis,
                TaskQueueService.getArtworkDownloadRetryPendingIntent(this));
    }
}

From source file:nu.yona.app.api.service.ActivityMonitorService.java

private void restartService() {
    Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass());
    restartServiceIntent.setPackage(getPackageName());

    PendingIntent restartServicePendingIntent = PendingIntent.getService(getApplicationContext(), 1,
            restartServiceIntent, PendingIntent.FLAG_ONE_SHOT);
    AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + AppConstant.ONE_SECOND,
            restartServicePendingIntent);

}

From source file:townley.stuart.app.android.trekkinly.Notification_picker_class.java

public void setNotification() {

    NotificationItemClass notificationItemClass = new NotificationItemClass();
    DataBaseHelper db = new DataBaseHelper(getActivity());

    db.getWritableDatabase();//from   w  w  w . ja  v  a2s  .co  m
    db.addNotification(notificationItemClass);
    db.upDateDataBaseNotification(notificationItemClass);
    db.getNotification(notificationItemClass);

    if (db.notification <= 0) {
        Toast.makeText(getActivity(), "Please set a time for the notification", Toast.LENGTH_SHORT).show();
    }

    else {

        ComponentName receiver = new ComponentName(getActivity(), AlertClass.class);
        PackageManager pm = getActivity().getPackageManager();
        pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);

        Intent intent = new Intent(getActivity(), AlertClass.class);
        AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, db.notification, PendingIntent.getBroadcast(
                getActivity().getApplicationContext(), 1, intent, PendingIntent.FLAG_UPDATE_CURRENT));
        Toast.makeText(getActivity(), "Notification issued", Toast.LENGTH_SHORT).show();
        Log.d("Trekkinly", "AlarmNotification Called");

    }
}

From source file:net.texh.cordovapluginstepcounter.StepCounterService.java

@Override
public boolean stopService(Intent intent) {
    Log.i(TAG, "- Received stop: " + intent);
    //Stop listening to events when stop() is called
    if (isRunning) {
        mSensorManager.unregisterListener(this);
        isRunning = false;//from w  w  w  .  j a v a2  s  .co m
    }

    SharedPreferences sharedPref = getSharedPreferences(CordovaStepCounter.USER_DATA_PREF,
            Context.MODE_PRIVATE);
    Boolean pActive = CordovaStepCounter.getPedometerIsActive(sharedPref);
    if (pActive) {
        Log.i(TAG, "- Relaunch service in 500ms");
        //Autorelaunch the service
        //@TODO should test if stopService is called from killing app or from calling stop() method in CordovaStepCounter
        Intent newServiceIntent = new Intent(this, StepCounterService.class);
        AlarmManager aManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        aManager.set(AlarmManager.RTC, java.lang.System.currentTimeMillis() + 500,
                PendingIntent.getService(this, 11, newServiceIntent, 0));
    } else {
        Log.i(TAG, "StepCounter stopped, will not relaunch service");
    }

    return super.stopService(intent);
}

From source file:com.packpublishing.asynchronousandroid.chapter6.SMSDispatchActivity.java

void launchService(String phoneMumber, String text, long time) {

    Intent intent = new Intent(this, SMSDispatcherIntentService.class);
    intent.putExtra(SMSDispatcherIntentService.TO_KEY, phoneMumber);
    intent.putExtra(SMSDispatcherIntentService.TEXT_KEY, text);
    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);

    PendingIntent service = PendingIntent.getService(getBaseContext(), 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    am.set(AlarmManager.RTC_WAKEUP, time, service);
}