T.Black
T.Black

Reputation: 25

Android Handler() Runnable , queue the Runnables up

Hello my fellow programmers :)
I call the function waitRunnable(int seconds, int startId) 4 times every time I click a button in my application. So waitRunnable() should just wait a variable amount of time and the time is set by a parameter seconds. But they run all at the same time so if the longest waitRunnable gets 10 seconds to wait and the other 3 waitRunnables wait less time then all 4 waitRunnables finish after 10 seconds but the first one should finish and just then the second one should be started so the total amount of time would be the sum of all parameters. I hope this is not to bad explained.

In love your jason <3 Thx for help :)

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

Upvotes: 0

Views: 802

Answers (1)

TheoKanning
TheoKanning

Reputation: 1639

You can use an ExecutorService to store a queue of Runnables and perform them one at a time. Call ExecutorService#submit to add a Runnable to the queue.

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

Upvotes: 1

Related Questions