Example usage for android.app AlarmManager setRepeating

List of usage examples for android.app AlarmManager setRepeating

Introduction

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

Prototype

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

Source Link

Document

Schedule a repeating alarm.

Usage

From source file:com.remdo.app.MainActivity.java

/**
 * Anables geopositioning service with configured minutes span in Services database
 *//*w  w w  . j av  a2  s. c om*/
private void enableGeopositioning() {
    mGeoInterval = dm.getServcieMinutes("Geo");
    long milisegundos = mGeoInterval * 60 * 1000;

    Intent intent = new Intent(this, GeopositioningService.class);
    pendingGeoIntent = PendingIntent.getService(this, 0, intent, 0);

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.add(Calendar.SECOND, 10);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), milisegundos,
            pendingGeoIntent);

    Toast.makeText(this, getString(R.string.started_geo_service), Toast.LENGTH_LONG).show();
}

From source file:com.remdo.app.MainActivity.java

/**
 * Enables Alerts service with configured minutes span in Services database
 *///from   w w  w  .jav a  2s  .  co  m
private void enableNotificationsService() {
    mAlertsInterval = dm.getServcieMinutes("Alerts");
    long milisegundos = mAlertsInterval * 60 * 1000;

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

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.add(Calendar.SECOND, 10);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), milisegundos,
            pendingAlertsIntent);

    Toast.makeText(this, getString(R.string.started_alerts_service), Toast.LENGTH_LONG).show();
}

From source file:jieehd.villain.updater.VillainUpdater.java

public void setRepeatingAlarm() {
    AlarmManager am;
    am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(this, checkInBackground.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), (3 * AlarmManager.INTERVAL_DAY),
            pendingIntent);//from  ww w  .  j  av a  2  s .c o m
}

From source file:org.wso2.emm.agent.services.operation.OperationManagerWorkProfile.java

@Override
public void restrictAccessToApplications(Operation operation) throws AndroidAgentException {
    AppRestriction appRestriction = CommonUtils.getAppRestrictionTypeAndList(operation, getResultBuilder(),
            getContextResources());//from  ww  w . j av a2  s  . co m

    if (Constants.AppRestriction.BLACK_LIST.equals(appRestriction.getRestrictionType())) {
        Intent restrictionIntent = new Intent(getContext(), AppLockService.class);
        restrictionIntent.setAction(Constants.APP_LOCK_SERVICE);

        restrictionIntent.putStringArrayListExtra(Constants.AppRestriction.APP_LIST,
                (ArrayList) appRestriction.getRestrictedList());

        PendingIntent pendingIntent = PendingIntent.getService(getContext(), 0, restrictionIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager alarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.add(Calendar.SECOND, 1); // First time
        long frequency = 1 * 1000; // In ms
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), frequency,
                pendingIntent);

        getContext().startService(restrictionIntent);

    }
    operation.setStatus(getContextResources().getString(R.string.operation_value_completed));
    getResultBuilder().build(operation);
}

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 w ww  .  j  a v a 2  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:org.gnucash.android.ui.transaction.TransactionFormFragment.java

/**
 * Collects information from the fragment views and uses it to create 
 * and save a transaction//ww  w  .j a v  a 2  s. com
 */
private void saveNewTransaction() {
    Calendar cal = new GregorianCalendar(mDate.get(Calendar.YEAR), mDate.get(Calendar.MONTH),
            mDate.get(Calendar.DAY_OF_MONTH), mTime.get(Calendar.HOUR_OF_DAY), mTime.get(Calendar.MINUTE),
            mTime.get(Calendar.SECOND));
    String name = mNameEditText.getText().toString();
    String description = mDescriptionEditText.getText().toString();
    BigDecimal amountBigd = parseInputToDecimal(mAmountEditText.getText().toString());

    long accountID = ((TransactionsActivity) getSherlockActivity()).getCurrentAccountID();
    Currency currency = Currency.getInstance(mTransactionsDbAdapter.getCurrencyCode(accountID));
    Money amount = new Money(amountBigd, currency);
    TransactionType type;
    if (mAccountType.hasDebitNormalBalance()) {
        type = amount.isNegative() ? TransactionType.CREDIT : TransactionType.DEBIT;
    } else
        type = amount.isNegative() ? TransactionType.DEBIT : TransactionType.CREDIT;
    if (mTransaction != null) {
        mTransaction.setAmount(amount);
        mTransaction.setName(name);
        mTransaction.setTransactionType(type);
    } else {
        mTransaction = new Transaction(amount, name, type);
    }

    mTransaction.setAccountUID(mTransactionsDbAdapter.getAccountUID(accountID));
    mTransaction.setTime(cal.getTimeInMillis());
    mTransaction.setDescription(description);

    //set the double account
    if (mUseDoubleEntry) {
        long doubleAccountId = mDoubleAccountSpinner.getSelectedItemId();
        //negate the transaction before saving if we are in the double account
        if (isInDoubleAccount()) {
            mTransaction.setAmount(amount.negate());
            mTransaction.setAccountUID(mTransactionsDbAdapter.getAccountUID(doubleAccountId));
            mTransaction.setDoubleEntryAccountUID(mTransactionsDbAdapter.getAccountUID(accountID));
        } else {
            mTransaction.setAccountUID(mTransactionsDbAdapter.getAccountUID(accountID));
            mTransaction.setDoubleEntryAccountUID(mTransactionsDbAdapter.getAccountUID(doubleAccountId));
        }
    }
    //save the normal transaction first
    mTransactionsDbAdapter.addTransaction(mTransaction);

    //set up recurring transaction if requested
    int recurrenceIndex = mRecurringTransactionSpinner.getSelectedItemPosition();
    if (recurrenceIndex != 0) {
        String[] recurrenceOptions = getResources().getStringArray(R.array.recurrence_period_millis);
        long recurrencePeriodMillis = Long.parseLong(recurrenceOptions[recurrenceIndex]);
        long firstRunMillis = System.currentTimeMillis() + recurrencePeriodMillis;

        Transaction recurringTransaction = new Transaction(mTransaction, true);
        recurringTransaction.setRecurrencePeriod(recurrencePeriodMillis);
        long recurringTransactionId = mTransactionsDbAdapter.addTransaction(recurringTransaction);

        PendingIntent recurringPendingIntent = PendingIntent.getBroadcast(getActivity().getApplicationContext(),
                (int) recurringTransactionId, Transaction.createIntent(mTransaction),
                PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, firstRunMillis, recurrencePeriodMillis,
                recurringPendingIntent);
    }

    //update widgets, if any
    WidgetConfigurationActivity.updateAllWidgets(getActivity().getApplicationContext());

    finish();
}

From source file:org.wso2.cdm.agent.AuthenticationActivity.java

private void startLocalNotification(Float interval) {
    long firstTime = SystemClock.elapsedRealtime();
    firstTime += 1 * 1000;/*from  w ww .jav  a2  s  .  c  o m*/

    Intent downloader = new Intent(context, AlarmReceiver.class);
    PendingIntent recurringDownload = PendingIntent.getBroadcast(context, 0, downloader,
            PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Float seconds = interval;
    if (interval < 1.0) {

        alarms.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, seconds.intValue(),
                recurringDownload);
    } else {
        alarms.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, seconds.intValue(),
                recurringDownload);
    }

}

From source file:net.networksaremadeofstring.rhybudd.ZenossPoller.java

private void PollerCheck() {
    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
    Intent Poller = new Intent(this, ZenossPoller.class);

    if (settings.getBoolean("AllowBackgroundService", true)) {
        //Log.i("PollerCheck","Background scanning enabled!");
        Notifications.SendStickyNotification(this);

        Poller.putExtra("events", true);
        PendingIntent Monitoring = PendingIntent.getService(this, 0, Poller, PendingIntent.FLAG_UPDATE_CURRENT);//PendingIntent.FLAG_UPDATE_CURRENT
        am.cancel(Monitoring);/*ww  w  .j  a  v  a2s.c om*/
        try {
            am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, (long) 0,
                    Long.parseLong(settings.getString("BackgroundServiceDelay", "60")) * 1000, Monitoring);
        } catch (Exception e) {
            am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, (long) 0, 60000, Monitoring);
            BugSenseHandler.sendExceptionMessage("ZenossPoller", "PollerCheck", e);
        }
    } else {
        //Log.i("PollerCheck","Background scanning disabled!");
        Poller.putExtra("events", true);
        PendingIntent Monitoring = PendingIntent.getService(this, 0, Poller, PendingIntent.FLAG_UPDATE_CURRENT);//PendingIntent.FLAG_UPDATE_CURRENT
        am.cancel(Monitoring);
        mNM.cancel(Notifications.NOTIFICATION_POLLED_STICKY);
    }

    /*if(settings.getBoolean("refreshCache", true))
    {
       //Log.i("PollerCheck","Background cache refresh enabled!");
               
       Poller.putExtra("refreshCache", true);
       PendingIntent CacheRefresh = PendingIntent.getService(this, 1, Poller, PendingIntent.FLAG_UPDATE_CURRENT);
       am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, 10000, AlarmManager.INTERVAL_HOUR, CacheRefresh);
    }
    else
    {
       //Log.i("PollerCheck","Background cache refresh disabled!");
       Poller.putExtra("refreshCache", true);
       PendingIntent CacheRefresh = PendingIntent.getService(this, 1, Poller, PendingIntent.FLAG_UPDATE_CURRENT);
       am.cancel(CacheRefresh);
    }*/

    //We use a SyncAdapter now like good citizens
    try {
        if (settings.getBoolean("refreshCache", true)) {
            Poller.putExtra("refreshCache", true);
            PendingIntent CacheRefresh = PendingIntent.getService(this, 1, Poller,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            am.cancel(CacheRefresh);
            SharedPreferences.Editor editor = settings.edit();
            editor.putBoolean("refreshCache", false);
            editor.commit();
        }
    } catch (Exception e) {
        e.printStackTrace();
        BugSenseHandler.sendExceptionMessage("ZenossPoller", "cancel refresh cache poller", e);
    }
}

From source file:com.hhunj.hhudata.ForegroundService.java

protected void clockRing() {
    Intent intent = new Intent(this, RepeatingAlarm.class);

    PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, 0);

    long firstTime = m_workStartTime.getTime();
    long elaps = 24 * 60 * 60 * 1000;
    //long elaps = 15 * 1000;
    // Schedule the alarm!
    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
    am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, elaps, sender);

    // Tell the user about what we did.
    if (mToast != null) {
        mToast.cancel();/*from   ww w  .ja  v  a 2 s.  com*/
    }
    mToast = Toast.makeText(this, "R.string.repeating_scheduled", Toast.LENGTH_LONG);
    mToast.show();

}

From source file:org.wso2.emm.agent.services.operation.OperationManagerOlderSdk.java

@Override
public void restrictAccessToApplications(Operation operation) throws AndroidAgentException {

    AppRestriction appRestriction = CommonUtils.getAppRestrictionTypeAndList(operation, getResultBuilder(),
            getContextResources());/*from   w w w.j  a  va 2  s  .co m*/

    String ownershipType = Preference.getString(getContext(), Constants.DEVICE_TYPE);

    if (Constants.AppRestriction.WHITE_LIST.equals(appRestriction.getRestrictionType())) {
        if (Constants.OWNERSHIP_COPE.equals(ownershipType)) {

            List<String> installedAppPackages = CommonUtils.getInstalledAppPackages(getContext());

            List<String> toBeHideApps = new ArrayList<>(installedAppPackages);
            toBeHideApps.removeAll(appRestriction.getRestrictedList());
            for (String packageName : toBeHideApps) {
                CommonUtils.callSystemApp(getContext(), operation.getCode(), "false", packageName);
            }
        }
    } else if (Constants.AppRestriction.BLACK_LIST.equals(appRestriction.getRestrictionType())) {
        if (Constants.OWNERSHIP_BYOD.equals(ownershipType)) {
            Intent restrictionIntent = new Intent(getContext(), AppLockService.class);
            restrictionIntent.setAction(Constants.APP_LOCK_SERVICE);

            restrictionIntent.putStringArrayListExtra(Constants.AppRestriction.APP_LIST,
                    (ArrayList) appRestriction.getRestrictedList());

            PendingIntent pendingIntent = PendingIntent.getService(getContext(), 0, restrictionIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);

            AlarmManager alarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            calendar.add(Calendar.SECOND, 1); // First time
            long frequency = 1 * 1000; // In ms
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), frequency,
                    pendingIntent);

            getContext().startService(restrictionIntent);
        } else if (Constants.OWNERSHIP_COPE.equals(ownershipType)) {

            for (String packageName : appRestriction.getRestrictedList()) {
                CommonUtils.callSystemApp(getContext(), operation.getCode(), "false", packageName);
            }
        }

    }
    operation.setStatus(getContextResources().getString(R.string.operation_value_completed));
    getResultBuilder().build(operation);

}