Stevetro
Stevetro

Reputation: 1963

Run Handler Task in Android successively

atm i have a problem with my actual Android application.

For explanation:

At first i want to show a Text in a TextView Char by Char. This is my actual Code for this

tvIntro.setText("");
        final Handler textHandler = new Handler();

        for(int i=0; i<intro.length();i++){

            final int finalCount = i;
            textHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    tvIntro.setText(tvIntro.getText() + (intro.charAt(finalCount)+""));
                }
            }, 150 * i);

        }

After the whole text is displayed, i want to play a sound and continuously change the Color of the Screen for 5 Seconds. For this, my code is:

myBackground.setBackgroundColor(Color.RED);// set initial colour
        final Thread blink =  new Thread(new Runnable() {
            public void run() {
                while (getRunning()) {
                    try {
                        Thread.sleep(100);
                        if(start[0] !=1){
                            mp.start();
                            start[0] = 1;

                        }


                    }
                    catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    updateColor(myBackground);
                    whichColor = !whichColor;
                }
            }
        });

private void updateColor(final RelativeLayout myBackground) {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            if (whichColor)
                myBackground.setBackgroundColor(Color.RED);
            else
                myBackground.setBackgroundColor(Color.GREEN);
        }
    });
}

All the functions are working, but i want too finish the first handler, before the second handler is executed. Furthermore the Second handler should stop after x seconds.

I have some problems understanding how the handler and the Threads work. Would be nice if someone of you have a solution for me.

Upvotes: 3

Views: 1128

Answers (1)

Zeth
Zeth

Reputation: 96

To delay the performing of tasks until a specified thread (or threads) finishes, add this line immediately after the thread you wish to wait on:

myThread.join();

And then immediately follow it with the code you wish to run after it finishes.

For your second problem, you can set a variable to be the value (in milliseconds) for the amount of time you want to wait before ending the thread, and then decrease that value by 100 (or whatever amount you choose to tell it to sleep for) each time the code runs. Have a check for the value to be less than or equal to zero, and then if that condition returns true, end the thread with an interrupt. So basically:

long timeToRun = 5000, sleepTime = 100;

// Your code here...

Thread.sleep(sleepTime);
timeToRun -= sleepTime;
if(timeToRun <= 0) {
myThread.interrupt();
}

There are likely more graceful ways to accomplish this, but at the very least this should solve your problems.

Upvotes: 1

Related Questions