package aexp.persistentapp;
import android.app.Service;
import android.content.Intent;
import android.content.Context;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Handler;
import android.util.Log;
class BackgroundServiceTask implements Runnable {
private static final String LOG_TAG = "BackgroundService";
private Handler serviceHandler = null;
private int countDown = 0;
private long interval = 0L;
public BackgroundServiceTask( int countDown, long interval, Handler serviceHandler ) {
this.countDown = countDown;
this.interval = interval;
this.serviceHandler = serviceHandler;
}
public void run() {
Log.d( LOG_TAG, "Counter: "+countDown );
if( --countDown > 0 )
serviceHandler.postDelayed( this,interval );
}
}
public class BackgroundService extends Service {
private static final String LOG_TAG = "BackgroundService";
private Handler serviceHandler = new Handler();
private static final int COUNTDOWN_LIMIT = 10;
private static final long COUNTDOWN_INTERVAL = 3*1000L;
@Override
protected void onStart(int startId, Bundle arguments) {
super.onStart( startId, arguments );
Log.d( LOG_TAG, "onStart" );
BackgroundServiceTask bst = new BackgroundServiceTask( COUNTDOWN_LIMIT, COUNTDOWN_INTERVAL, serviceHandler );
serviceHandler.postDelayed( bst,COUNTDOWN_INTERVAL );
stopSelf();
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d( LOG_TAG, "onDestroy" );
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
评论4
最新资源