FileDocCategorySizeDatePackage
WakeLoopService.javaAPI DocAndroid 5.1 API3615Thu Mar 12 22:22:44 GMT 2015android.test.wakeuploop

WakeLoopService

public class WakeLoopService extends android.app.Service

Fields Summary
private static final String
LOG_TAG
static final String
WAKEUP_INTERNAL
static final String
MAX_LOOP
static final String
STOP_CALLBACK
static final String
THIS_LOOP
static final int
MSG_STOP_SERVICE
private final android.os.Handler
mHandler
Constructors Summary
Methods Summary
public android.os.IBinderonBind(android.content.Intent intent)


    
        
        // no binding, just start via intent
        return null;
    
public voidonDestroy()

        Log.d(LOG_TAG, "WakeLoop: STOPPED");
        // cancel alarms first
        Intent intent = new Intent(WakeUpCall.WAKEUP_CALL)
            .putExtra(WakeUpCall.CANCEL, "true");
        sendBroadcast(intent);
    
public intonStartCommand(android.content.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;