use of android.app.AlarmManager in project android_frameworks_base by crdroidandroid.
the class WakeLoopService method onStartCommand.
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// 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;
}
use of android.app.AlarmManager in project android_frameworks_base by crdroidandroid.
the class WakeUpCall method onReceive.
@Override
public void onReceive(Context context, 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));
}
}
use of android.app.AlarmManager in project android_frameworks_base by crdroidandroid.
the class DaylightHeaderProvider method stopAlarm.
private void stopAlarm() {
if (mAlarmIntent != null) {
final AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
if (DEBUG)
Log.i(TAG, "stop alarm");
alarmManager.cancel(mAlarmIntent);
}
mAlarmIntent = null;
}
use of android.app.AlarmManager in project android_frameworks_base by crdroidandroid.
the class DaylightHeaderProvider method startAlarm.
private void startAlarm() {
// TODO actually this should find out the next needed alarm
// instead of forcing it every interval
final Calendar c = Calendar.getInstance();
final AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
if (mAlarmIntent != null) {
alarmManager.cancel(mAlarmIntent);
}
Intent intent = new Intent(StatusBarHeaderMachine.STATUS_BAR_HEADER_UPDATE_ACTION);
mAlarmIntent = PendingIntent.getBroadcast(mContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
// make sure hourly alarm is aligned with hour
if (mAlarmIntervalMinutes == 0) {
c.add(Calendar.HOUR_OF_DAY, 1);
c.set(Calendar.MINUTE, 0);
}
long alarmStart = c.getTimeInMillis();
long interval = mAlarmIntervalMinutes == 0 ? AlarmManager.INTERVAL_HOUR : mAlarmIntervalMinutes * 60 * 1000;
if (DEBUG)
Log.i(TAG, "start alarm at " + new Date(alarmStart) + " with interval of " + interval);
alarmManager.setInexactRepeating(AlarmManager.RTC, alarmStart, interval, mAlarmIntent);
}
use of android.app.AlarmManager in project android_frameworks_base by crdroidandroid.
the class NetworkStatsService method create.
public static NetworkStatsService create(Context context, INetworkManagementService networkManager) {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
NetworkStatsService service = new NetworkStatsService(context, networkManager, alarmManager, wakeLock, NtpTrustedTime.getInstance(context), TelephonyManager.getDefault(), new DefaultNetworkStatsSettings(context), new NetworkStatsObservers(), getDefaultSystemDir(), getDefaultBaseDir());
HandlerThread handlerThread = new HandlerThread(TAG);
Handler.Callback callback = new HandlerCallback(service);
handlerThread.start();
Handler handler = new Handler(handlerThread.getLooper(), callback);
service.setHandler(handler, callback);
HandlerThread mStatsThread = new HandlerThread("StatsObserver");
mStatsThread.start();
Handler mStatsHandler = new Handler(mStatsThread.getLooper());
return service;
}
Aggregations