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:me.acristoffers.tracker.AlarmReceiver.java

public static void setAlarm(final Context context) {
    try {/*from w  w  w . j  a  v a 2 s  .com*/
        final Intent intent = new Intent(context, AlarmReceiver.class);
        final PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        final AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

        final long fiveMinutes = 5 * 60 * 1000;

        final SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
        final String minutes = sharedPref.getString("sync_interval", "15");

        long repeatMinutes;
        try {
            repeatMinutes = Long.parseLong(minutes) * 60 * 1000;
        } catch (NumberFormatException e) {
            repeatMinutes = 15 * 60 * 1000;
        }

        final boolean active = sharedPref.getBoolean("autosync", true);

        if (active) {
            alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, fiveMinutes, repeatMinutes,
                    pendingIntent);
        } else {
            alarmManager.cancel(pendingIntent);
        }
    } catch (NullPointerException e) {
        e.printStackTrace();
    }
}

From source file:com.cryart.sabbathschool.util.SSNotification.java

public static void setRepeatingNotification(Context context) {
    AlarmManager _SSAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent _SSNotificationIntent = new Intent(context, SSBroadcastReceiver.class);
    PendingIntent _SSAlarmIntent = PendingIntent.getBroadcast(context, 0, _SSNotificationIntent, 0);

    Calendar _SSNotificationTime = Calendar.getInstance();

    SharedPreferences ssPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    String ss_settings_notification_time = ssPreferences.getString(
            SSConstants.SS_SETTINGS_NOTIFICATION_TIME_KEY,
            SSConstants.SS_SETTINGS_NOTIFICATION_TIME_DEFAULT_VALUE);

    _SSNotificationTime.set(Calendar.HOUR_OF_DAY, SSHelper.parseHourFromString(ss_settings_notification_time,
            SSConstants.SS_NOTIFICATION_TIME_SETTINGS_FORMAT));
    _SSNotificationTime.set(Calendar.MINUTE, SSHelper.parseMinuteFromString(ss_settings_notification_time,
            SSConstants.SS_NOTIFICATION_TIME_SETTINGS_FORMAT));
    _SSNotificationTime.set(Calendar.SECOND, 0);

    _SSAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, _SSNotificationTime.getTimeInMillis(),
            AlarmManager.INTERVAL_DAY, _SSAlarmIntent);
}

From source file:me.oss.tracker.trackme.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    this.context = this;

    textView = (TextView) findViewById(R.id.deviceID);

    /* Handset mac*/
    TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    String device_id = tm.getDeviceId();
    textView.setText(device_id + "");

    Intent alarm = new Intent(this.context, AlarmReceiver.class);
    boolean alarmRunning = (PendingIntent.getBroadcast(this.context, 0, alarm,
            PendingIntent.FLAG_NO_CREATE) != null);
    if (alarmRunning == false) {
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this.context, 0, alarm, 0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(),
                LocalLog.syn_time, pendingIntent);
    }//from w  w  w  .  jav a  2 s  .c om

    mRegistrationProgressBar = (ProgressBar) findViewById(R.id.registrationProgressBar);
    mRegistrationBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            mRegistrationProgressBar.setVisibility(ProgressBar.GONE);
            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
            boolean sentToken = sharedPreferences.getBoolean(QuickstartPreferences.SENT_TOKEN_TO_SERVER, false);
            if (sentToken) {
                mInformationTextView.setText(getString(R.string.gcm_send_message));
            } else {
                mInformationTextView.setText(getString(R.string.token_error_message));
            }
        }
    };
    mInformationTextView = (TextView) findViewById(R.id.informationTextView);

    if (checkPlayServices()) {
        // Start IntentService to register this application with GCM.
        Intent intent = new Intent(this, RegistrationIntentService.class);
        startService(intent);
    }
}

From source file:org.wso2.emm.agent.services.DeviceStartupIntentReceiver.java

/**
 * Initiates device notifier on device startup.
 * @param context - Application context.
 *//* w w  w  . ja va  2 s  . co m*/
private void setRecurringAlarm(Context context) {
    this.resources = context.getApplicationContext().getResources();
    String mode = Preference.getString(context, Constants.PreferenceFlag.NOTIFIER_TYPE);
    boolean isLocked = Preference.getBoolean(context, Constants.IS_LOCKED);
    String lockMessage = Preference.getString(context, Constants.LOCK_MESSAGE);

    if (lockMessage == null || lockMessage.isEmpty()) {
        lockMessage = resources.getString(R.string.txt_lock_activity);
    }

    if (isLocked) {
        Operation lockOperation = new Operation();
        lockOperation.setId(DEFAULT_ID);
        lockOperation.setCode(Constants.Operation.DEVICE_LOCK);
        try {
            JSONObject payload = new JSONObject();
            payload.put(Constants.ADMIN_MESSAGE, lockMessage);
            payload.put(Constants.IS_HARD_LOCK_ENABLED, true);
            lockOperation.setPayLoad(payload.toString());
            OperationProcessor operationProcessor = new OperationProcessor(context);
            operationProcessor.doTask(lockOperation);
        } catch (AndroidAgentException e) {
            Log.e(TAG, "Error occurred while executing hard lock operation at the device startup");
        } catch (JSONException e) {
            Log.e(TAG, "Error occurred while building hard lock operation payload");
        }
    }

    int interval = Preference.getInt(context, context.getResources().getString(R.string.shared_pref_frequency));
    if (interval == DEFAULT_INDEX) {
        interval = DEFAULT_INTERVAL;
    }

    if (mode == null) {
        mode = Constants.NOTIFIER_LOCAL;
    }

    if (Preference.getBoolean(context, Constants.PreferenceFlag.REGISTERED)
            && Constants.NOTIFIER_LOCAL.equals(mode.trim().toUpperCase(Locale.ENGLISH))) {
        long startTime = DEFAULT_TIME_MILLISECONDS;

        Intent alarmIntent = new Intent(context, AlarmReceiver.class);
        PendingIntent recurringAlarmIntent = PendingIntent.getBroadcast(context, DEFAULT_REQUEST_CODE,
                alarmIntent, PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, startTime, interval,
                recurringAlarmIntent);
        Log.d(TAG, "Setting up alarm manager for polling every " + interval + " milliseconds.");
    }
}

From source file:com.capstone.transit.trans_it.RouteMap.java

@Override
protected void onResume() {
    super.onResume();
    setUpMapIfNeeded();/*from  w ww .  j  ava2s  .c om*/
    positionsServiceIntent = new Intent(getApplicationContext(), RefreshPositionsService.class);
    positionsServiceIntent.putExtra("EXTRA_ROUTE_ID", routeID);
    positionsServiceIntent.putExtra("EXTRA_RECEIVER", new PositionsReceiver(new Handler()));
    final PendingIntent pendingIntent = PendingIntent.getService(this, 0, positionsServiceIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    long trigger = System.currentTimeMillis();
    int intervalMillis = 1000 * 60;
    AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
    alarm.setRepeating(AlarmManager.RTC, trigger, intervalMillis, pendingIntent);
}

From source file:com.battlelancer.seriesguide.appwidget.ListWidgetProvider.java

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    super.onUpdate(context, appWidgetManager, appWidgetIds);

    // update all added list widgets
    for (int appWidgetId : appWidgetIds) {
        onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, null);
    }//from   ww  w .  j  a v  a2s .  c o  m

    // set an alarm to update widgets every x mins if the device is awake
    Intent update = new Intent(UPDATE);
    PendingIntent pi = PendingIntent.getBroadcast(context, 195, update, 0);

    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    am.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + REPETITION_INTERVAL,
            REPETITION_INTERVAL, pi);
}

From source file:org.wso2.iot.agent.services.AgentStartupReceiver.java

/**
 * Initiates device notifier on device startup.
 * @param context - Application context.
 *//*from   w w  w.j a v a2s. c o m*/
private void setRecurringAlarm(Context context) {
    Resources resources = context.getApplicationContext().getResources();
    String mode = Preference.getString(context, Constants.PreferenceFlag.NOTIFIER_TYPE);
    boolean isLocked = Preference.getBoolean(context, Constants.IS_LOCKED);
    String lockMessage = Preference.getString(context, Constants.LOCK_MESSAGE);

    if (lockMessage == null || lockMessage.isEmpty()) {
        lockMessage = resources.getString(R.string.txt_lock_activity);
    }

    if (isLocked) {
        Operation lockOperation = new Operation();
        lockOperation.setId(DEFAULT_ID);
        lockOperation.setCode(Constants.Operation.DEVICE_LOCK);
        try {
            JSONObject payload = new JSONObject();
            payload.put(Constants.ADMIN_MESSAGE, lockMessage);
            payload.put(Constants.IS_HARD_LOCK_ENABLED, true);
            lockOperation.setPayLoad(payload.toString());
            OperationProcessor operationProcessor = new OperationProcessor(context);
            operationProcessor.doTask(lockOperation);
        } catch (AndroidAgentException e) {
            Log.e(TAG, "Error occurred while executing hard lock operation at the device startup");
        } catch (JSONException e) {
            Log.e(TAG, "Error occurred while building hard lock operation payload");
        }
    }

    int interval = Preference.getInt(context, context.getResources().getString(R.string.shared_pref_frequency));
    if (interval == DEFAULT_INT_VALUE) {
        interval = Constants.DEFAULT_INTERVAL;
    }

    if (mode == null) {
        mode = Constants.NOTIFIER_LOCAL;
    }

    if (Preference.getBoolean(context, Constants.PreferenceFlag.REGISTERED)
            && Constants.NOTIFIER_LOCAL.equals(mode.trim().toUpperCase(Locale.ENGLISH))) {

        Intent alarmIntent = new Intent(context, AlarmReceiver.class);
        PendingIntent recurringAlarmIntent = PendingIntent.getBroadcast(context, Constants.DEFAULT_REQUEST_CODE,
                alarmIntent, PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, Constants.DEFAULT_START_INTERVAL,
                interval, recurringAlarmIntent);
        Log.i(TAG, "Setting up alarm manager for polling every " + interval + " milliseconds.");
    }
}

From source file:org.roman.findme.MainActivity.java

void startLocationService() {
    //        Intent intent = new Intent(this, AndroidLocationServices.class);
    //        startService(intent);

    Intent intent = new Intent(this, AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1253, intent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarm.cancel(pendingIntent);/*from   ww  w  . j av a2s . c  o m*/
    alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_HOUR,
            pendingIntent);

}

From source file:com.hedgehog.smdb.ActionBarControlScrollViewActivity.java

private void repeat() {
    BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override//from  w ww .j a v a  2 s .  c  o m
        public void onReceive(Context context, Intent intent) {
            showNotification();
        }
    };

    this.registerReceiver(receiver, new IntentFilter("TecxiDriverCheckingForConfirmedBids"));
    PendingIntent pintent = PendingIntent.getBroadcast(this, 0,
            new Intent("TecxiDriverCheckingForConfirmedBids"), 0);
    AlarmManager manager = (AlarmManager) (this.getSystemService(Context.ALARM_SERVICE));
    manager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 0, 1000 * 60 * 30, pintent);
}

From source file:com.macleod2486.utnow.MainActivity.java

@Override
public void onStop() {
    //Start the service in a timely interval
    SharedPreferences shared = PreferenceManager.getDefaultSharedPreferences(this);

    if (shared.getBoolean("notification", false) && shared.getBoolean("notifCancel", true)) {
        //one second * 60 seconds in a minute * 5
        long fiveMinutes = 1000 * 60 * 5;

        //Start the alarm manager service
        SharedPreferences.Editor edit = shared.edit();
        Intent service = new Intent(getApplicationContext(), UTBroadcast.class);
        PendingIntent pendingService = PendingIntent.getBroadcast(getApplicationContext(), 0, service, 0);
        AlarmManager newsUpdate = (AlarmManager) getSystemService(ALARM_SERVICE);

        //Check for the update every 5 minutes
        newsUpdate.setRepeating(AlarmManager.RTC, System.currentTimeMillis(), fiveMinutes, pendingService);
        edit.putBoolean("notifCancel", false).commit();
        Log.i("UTService", "Alarm set " + shared.getBoolean("notification", true));
    }//  w  ww.ja v a  2 s.c o  m

    super.onStop();
}