Android和Java的队列相关类:Tape

jopen 10年前

Tape是速度非快,事务性,基于文件的FIFO。用于Android和Java平台。

Android Task Queue Service

When used on Android, a service is the perfect companion to a TaskQueue since it allows actions to be completed in the background. If the user is uploading new photos to their favorite sharing site, the service will iterate through the queue until all of the upload tasks completes successfully.

/** Listener for starting the upload service when the queue has tasks. */  public class ImageQueueServiceListener implements ObjectQueue.Listener<ImageUploadTask> {    private final Context context;      public ImageQueueServiceStarter(Context context) {      this.context = context;    }      @Override public void onAdd(ObjectQueue<ImageUploadTask>, ImageUploadTask task) {      context.startService(new Intent(context, ImageQueueService.class));    }      @Override public void onRemove(ObjectQueue<ImageUploadTask>) {}  }    /** Service which iterates through pending upload tasks one-by-one. */  public class ImageQueueService extends Service implements ImageUploadTask.Callback {    private TaskQueue<ImageUploadTask> queue;    private boolean running;      @Override public void onCreate() {      super.onCreate();      // Obtain TaskQueue here (e.g., through injection)    }      @Override public int onStartCommand(Intent intent, int flags, int startId) {      executeNext();      return START_STICKY;    }      public void executeNext() {      if (running) return; // Only one task at a time.      ImageUploadTask task = queue.peek();      if (task != null) {        task.execute(this);        running = true;        return;      }      stopSelf(); // We're done for now.    }      @Override public void imageUploadComplete() {      running = false;      queue.remove();      executeNext();    }  }

项目主页:http://www.open-open.com/lib/view/home/1383140217077