Android和Java的隊列相關類:Tape

jopen 11年前發布 | 34K 次閱讀 Tape Java開發

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.baiduhome.net/lib/view/home/1383140217077

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!