Code examples for Creating a periodic task

Codota analyzed 7,668,838 open source code samples to find the most common, credible and clear examples.
You can now enable Codota on your own code to easily search and navigate your Java codebase.
 
        Log.i("Astrid", "Autosync set for " + offset / 1000 //$NON-NLS-1$ //$NON-NLS-2$ 
            + " seconds repeating every " + syncFrequencySeconds); //$NON-NLS-1$ 
 
        // cancel all existing 
        am.cancel(pendingIntent); 
 
        // schedule new 
        am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + offset, 
                interval, pendingIntent); 
    } 
 
 
    /** 
     * Removes repeating alarm for auto-synchronization 
     */ 
    private void unscheduleService(Context context) { 
        AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
        PendingIntent pendingIntent = PendingIntent.getService(context, getSyncUtilities().getSyncIntervalKey(), 
                createAlarmIntent(context), PendingIntent.FLAG_UPDATE_CURRENT); 
Full Snippet Info
7
CodeRank
    AlarmManager mgr=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
    Intent i=new Intent(context, OnAlarmReceiver.class); 
    PendingIntent pi=PendingIntent.getBroadcast(context, 0, 
                                              i, 0); 
     
    mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
                      SystemClock.elapsedRealtime()+60000, 
                      PERIOD, 
                      pi); 
  } 
} 
Full Snippet Info
7
CodeRank
    public static void resetAlarm(Context context, long interval) { 
        // Create an IntentSender that will launch our service, to be scheduled 
        // with the alarm manager. 
        PendingIntent alarmSender = PendingIntent.getService(context, 0, new Intent(context, EnvelopeService.class), 0); 
        AlarmManager am = (AlarmManager) context.getSystemService(ALARM_SERVICE); 
        am.cancel(alarmSender); 
        if (interval != 0) 
        	am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), interval, alarmSender); 
    } 
} 
 
Full Snippet Info
7
CodeRank
					this.appSettings.getString("autoSyncInterval", "1800000"), 
					10); 
 
			this.alarmIntent = PendingIntent.getService(appInst, 0, new Intent( 
					this, SyncService.class), 0); 
			alarmManager.setRepeating(AlarmManager.RTC, 
					System.currentTimeMillis() + interval, interval, 
					alarmIntent); 
 
			this.alarmScheduled = true; 
		} 
	} 
 
	private void unsetAlarm() { 
		if (this.alarmScheduled) { 
			this.alarmManager.cancel(this.alarmIntent); 
			this.alarmScheduled = false; 
		} 
	} 
 
	private void resetAlarm() { 
Full Snippet Info
7
CodeRank
 
		if (command.contains("Start Alarm")) { 
			alarmManager.set(AlarmManager.RTC_WAKEUP, time, pi); 
		} else if (command.contains("Stop Alarm")) { 
			pi.cancel(); 					// Cancel the pendingIntent. 
			alarmManager.cancel(pi); 		// Cancel the alarm. 
			 
			// If more times with a timeReminder exist, start a new alarm. 
			if (time != 0) alarmManager.set(AlarmManager.RTC_WAKEUP, time, pi); 
		} else 
			Log.e("AlarmManagerService", "Command contained unknown value"); 
	} 
 
	/** 
	 * Method for starting or stopping the position alarm. 
	 * @param command decides whether to start or stop the alarm. 
	 */ 
	private void positionAlarm(String command) { 
		// Let the receiver know its a position-alarm. 
		alarmReceiverIntent.putExtra("alarmType", "position"); 

7
CodeRank
    AlarmManager mgr=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
    Intent i=new Intent(context, OnAlarmReceiver.class); 
    PendingIntent pi=PendingIntent.getBroadcast(context, 0, 
                                              i, 0); 
     
    mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
                      SystemClock.elapsedRealtime()+60000, 
                      PERIOD, 
                      pi); 
  } 
} 
Full Snippet Info
7
CodeRank
 
    if (cal.getTimeInMillis() < System.currentTimeMillis()) { 
      cal.add(Calendar.DAY_OF_YEAR, 1); 
    } 
 
    mgr.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 
                     AlarmManager.INTERVAL_DAY, pi); 
  } 
} 
Full Snippet Info
7
CodeRank
    AlarmManager mgr= 
        (AlarmManager)ctxt.getSystemService(Context.ALARM_SERVICE); 
    Intent i=new Intent(ctxt, ScheduledService.class); 
    PendingIntent pi=PendingIntent.getService(ctxt, 0, i, 0); 
 
    mgr.setRepeating(AlarmManager.ELAPSED_REALTIME, 
                     SystemClock.elapsedRealtime() + PERIOD, PERIOD, pi); 
  } 
} 
Full Snippet Info
6
CodeRank
    AlarmManager mgr= 
        (AlarmManager)ctxt.getSystemService(Context.ALARM_SERVICE); 
    Intent i=new Intent(ctxt, PollReceiver.class); 
    PendingIntent pi=PendingIntent.getBroadcast(ctxt, 0, i, 0); 
 
    mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
                     SystemClock.elapsedRealtime() + INITIAL_DELAY, 
                     PERIOD, pi); 
 
  } 
} 
Full Snippet Info
6
CodeRank
	private void setUpdateAlarm() { 
		Intent intent = new Intent(this, TimeclockService.class); 
		intent.putExtra("action", TIMECLOCK_UPDATE); 
		this.updateIntent = PendingIntent.getService(appInst, 1, 
				intent, 0); 
		alarmManager.setRepeating(AlarmManager.RTC, System.currentTimeMillis() 
				+ DateUtils.MINUTE_IN_MILLIS, DateUtils.MINUTE_IN_MILLIS, updateIntent); 
	} 
	 
	private void setTimeoutAlarm(int hour, int minute) { 
		if(hour <= 0 && minute <= 0) 
			return; 
		 
		long time = (hour * DateUtils.HOUR_IN_MILLIS) 
				+ (minute * DateUtils.MINUTE_IN_MILLIS); 
		 
		Intent intent = new Intent(this, TimeclockService.class); 
		intent.putExtra("action", TIMECLOCK_TIMEOUT); 
		this.timeoutIntent = PendingIntent.getService(appInst, 2, 
				intent, 0); 
Full Snippet Info
6
CodeRank