Android Handler() Runnable ,将 Runnables 排队

Android Handler() Runnable , queue the Runnables up

各位程序员大家好:)
每次单击应用程序中的按钮时,我都会调用函数 waitRunnable(int seconds, int startId) 4 次。所以 waitRunnable() 应该只等待可变的时间量,时间由参数秒设置。但是它们 运行 都是同时进行的,所以如果最长的 waitRunnable 等待 10 秒,而其他 3 个 waitRunnable 等待的时间更短,那么所有 4 个 waitRunnable 都会在 10 秒后完成,但第一个应该完成,然后第二个应该开始所以总时间量将是所有参数的总和。我希望这解释得还不错。

爱上你的杰森 <3 谢谢你的帮助:)

package com.example.Uebung10;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.os.Handler;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * Created by Jason on 14.01.2016.
 */
public class MyService extends Service {
    final String LOG_TAG = "myServiceLogs";
    Handler h = new Handler();
    List<String> finishedTasksInTheLast60Sec = new ArrayList<>();
    ExecutorService es;
    Runnable r = new Runnable() {
        @Override
        public void run() {
            sendBroadcast(finishedTasksInTheLast60Sec);
            h.postDelayed(this, 60000);
            finishedTasksInTheLast60Sec = new ArrayList<>();
        }
    };

    private void waitRunnable(int seconds, int startId) {
        h.postDelayed(new Runnable() {
            @Override
            public void run() {
                finishedTasksInTheLast60Sec.add("Finished Task: MyRun#" + startId);
                Log.d(LOG_TAG, "MyRun#" + startId + " end");
            }
        }, TimeUnit.SECONDS.toMillis(seconds));
    }

    private void sendBroadcast(List<String> finishedTasks) {
        Intent intent = new Intent("myServiceUpdate");
        intent.putExtra("finishedTasks", (ArrayList<String>)finishedTasks);
        LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
    }

    public void onCreate() {
        super.onCreate();
        Log.d(LOG_TAG, "MyService onCreate");
        es = Executors.newFixedThreadPool(1);
        h.postDelayed(r, 60000);

    }

    public void onDestroy() {
        super.onDestroy();
        h.removeCallbacks(r);
        Log.d(LOG_TAG, "MyService onDestroy ");
    }

    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(LOG_TAG, "MyService onStartCommand");
        readFlags(flags);
        int time = intent.getIntExtra("time", 1);
        if (time != -1) {
            MyRun mr = new MyRun(time, startId);
            es.execute(mr);
        } else stopSelf();
        return START_NOT_STICKY;
        //return START_STICKY;
        //return START_REDELIVER_INTENT;
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.d(LOG_TAG, "onBind");
        return null;
    }

    void readFlags(int flags) {
        if ((flags & START_FLAG_REDELIVERY) == START_FLAG_REDELIVERY)
            Log.d(LOG_TAG, "START_FLAG_REDELIVERY");
        if ((flags & START_FLAG_RETRY) == START_FLAG_RETRY)
            Log.d(LOG_TAG, "START_FLAG_RETRY");
    }

    class MyRun implements Runnable {
        int time;
        int startId;

        public MyRun(int time, int startId) {
            this.time = time;
            this.startId = startId;
            Log.d(LOG_TAG, "MyRun#" + startId + " create");
        }

        @Override
        public void run() {
            Log.d(LOG_TAG, "MyRun#" + startId + " start, time = " + time);
            waitRunnable(time, startId);
        }
    }
}

您可以使用 ExecutorService 来存储 Runnables 队列并一次执行一个。调用 ExecutorService#submitRunnable 添加到队列中。

http://developer.android.com/reference/java/util/concurrent/ExecutorService.html