public synchronized void startPeriodicReplication ( ) { if ( ! isPeriodicReplicationEnabled ( ) ) { setPeriodicReplicationEnabled ( true ) ; AlarmManager alarmManager = ( AlarmManager ) getSystemService ( Context . ALARM_SERVICE ) ; Intent alarmIntent = new Intent ( this , clazz ) ; alarmIntent . setAction ( PeriodicReplicationReceiver . ALARM_ACTION ) ; // We need to use a BroadcastReceiver rather than sending the Intent directly to the // Service to ensure the device wakes up if it's asleep. Sending the Intent directly // to the Service would be unreliable. PendingIntent pendingAlarmIntent = PendingIntent . getBroadcast ( this , 0 , alarmIntent , 0 ) ; long initialTriggerTime ; if ( explicitlyStopped ( ) ) { // Replications were explicitly stopped, so we want the first replication to // happen immediately. initialTriggerTime = SystemClock . elapsedRealtime ( ) ; setExplicitlyStopped ( false ) ; } else { // Replications were implicitly stopped (e.g. by rebooting the device), so we // want to resume the previous schedule. initialTriggerTime = getNextAlarmDueElapsedTime ( ) ; } alarmManager . setInexactRepeating ( AlarmManager . ELAPSED_REALTIME_WAKEUP , initialTriggerTime , getIntervalInSeconds ( ) * MILLISECONDS_IN_SECOND , pendingAlarmIntent ) ; } else { Log . i ( TAG , "Attempted to start an already running alarm manager" ) ; } }
Start periodic replications .