WakeUpCallpublic class WakeUpCall extends android.content.BroadcastReceiver The receiver for the alarm we set |
Fields Summary |
---|
private static final String | LOG_TAG | static final String | WAKEUP_CALL | static final String | CANCEL |
Methods Summary |
---|
public void | onReceive(android.content.Context context, android.content.Intent intent)
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
boolean cancel = intent.hasExtra(CANCEL);
if (!cancel) {
long maxLoop = intent.getLongExtra(WakeLoopService.MAX_LOOP, 0);
long wakeupInterval = intent.getLongExtra(WakeLoopService.WAKEUP_INTERNAL, 0);
long thisLoop = intent.getLongExtra(WakeLoopService.THIS_LOOP, -1);
Log.d(LOG_TAG, String.format("incoming: interval = %d, max loop = %d, this loop = %d",
wakeupInterval, maxLoop, thisLoop));
if (thisLoop == -1) {
Log.e(LOG_TAG, "no valid loop count received, trying to stop service");
stopService(intent);
return;
}
if (wakeupInterval == 0) {
Log.e(LOG_TAG, "no valid wakeup interval received, trying to stop service");
stopService(intent);
return;
}
thisLoop++;
Log.d(LOG_TAG, String.format("WakeLoop - iteration %d of %d", thisLoop, maxLoop));
if (thisLoop == maxLoop) {
// when maxLoop is 0, we loop forever, so not checking that case
// here
Log.d(LOG_TAG, "reached max loop count, stopping service");
stopService(intent);
return;
}
screenOn(context);
FileUtil.get().writeDateToFile(
new File(Environment.getExternalStorageDirectory(), "wakeup-loop.txt"));
// calculate when device should be waken up
long atTime = SystemClock.elapsedRealtime() + wakeupInterval;
intent.putExtra(WakeLoopService.THIS_LOOP, thisLoop);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
// set alarm, which will be delivered in form of the wakeupIntent
am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, atTime, pi);
} else {
// cancel alarms
Log.d(LOG_TAG, "cancelling future alarms on request");
am.cancel(PendingIntent.getBroadcast(context, 0, intent, 0));
}
| private void | screenOn(android.content.Context context)
PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
@SuppressWarnings("deprecation")
WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK |
PowerManager.ACQUIRE_CAUSES_WAKEUP, LOG_TAG);
wl.acquire(500);
| private void | stopService(android.content.Intent i)
Messenger msgr = i.getParcelableExtra(WakeLoopService.STOP_CALLBACK);
if (msgr == null) {
Log.e(LOG_TAG, "no stop service callback found, cannot stop");
} else {
Message msg = new Message();
msg.what = WakeLoopService.MSG_STOP_SERVICE;
try {
msgr.send(msg);
} catch (RemoteException e) {
Log.e(LOG_TAG, "ignored remoted exception while attempting to stop service", e);
}
}
|
|