Android Handler()Runnable,将Runnables排队

时间:2016-01-20 20:50:47

标签: android handler runnable

你好我的同事程序员:)
每次单击应用程序中的按钮时,我都会调用函数waitRunnable(int seconds,int startId)4次。所以waitRunnable()应该等待一段可变的时间,并且时间由参数秒设置。但是它们同时运行所以如果最长的waitRunnable等待10秒钟而另外3个waitRunnables等待的时间更短,那么所有4个waitRunnables在10秒后完成但是第一个应该完成,然后第二个应该启动所以总时间量是所有参数的总和。我希望这不是坏事。

爱你的jason< 3 Thx求助:)

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);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您可以使用ExecutorService来存储Runnables的队列,并一次执行一个。{1}}。致电ExecutorService#submitRunnable添加到队列中。

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