// get wakeup interval from intent
long wakeupInterval = intent.getLongExtra(WAKEUP_INTERNAL, 0);
long maxLoop = intent.getLongExtra(MAX_LOOP, 0);
if (wakeupInterval == 0) {
// stop and error
Log.e(LOG_TAG, "No wakeup interval specified, not starting the service");
stopSelf();
return START_NOT_STICKY;
}
FileUtil.get().writeDateToFile(new File(Environment.getExternalStorageDirectory(),
"wakeup-loop-start.txt"));
Log.d(LOG_TAG, String.format("WakeLoop: STARTED interval = %d, total loop = %d",
wakeupInterval, maxLoop));
// calculate when device should be waken up
long atTime = SystemClock.elapsedRealtime() + wakeupInterval;
AlarmManager am = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent wakupIntent = new Intent(WakeUpCall.WAKEUP_CALL)
.putExtra(WAKEUP_INTERNAL, wakeupInterval)
.putExtra(MAX_LOOP, maxLoop)
.putExtra(THIS_LOOP, 0L)
.putExtra(STOP_CALLBACK, new Messenger(mHandler));
PendingIntent pi = PendingIntent.getBroadcast(this, 0, wakupIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
// set alarm, which will be delivered in form of the wakeupIntent
am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, atTime, pi);
return START_NOT_STICKY;