Вопрос

I found this code on the net. I am a newbie so i don't know much about java and android handler. I know how to use Runnables in handler to post on ui thread. But In this code use of handler.sendMessage(handler.obtainMessage)I didn't get it. I also confused about difference between atomic boolean and boolean. So please exaplain this. help is appreciated.

package com.example.watch;
import java.util.Calendar;
import java.util.concurrent.atomic.AtomicBoolean;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
import android.app.Activity;

public class MainActivity extends Activity {

    TextView watch;
    AtomicBoolean ContinueThread = new AtomicBoolean(false);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        watch = (TextView) findViewById(R.id.txtWatch);
        displayCurrentTime();
    }

    public void displayCurrentTime() {

        Calendar c = Calendar.getInstance();
        // String curTime = String.valueOf(c.getTime());
        int hours = c.get(Calendar.HOUR);
        int minutes = c.get(Calendar.MINUTE);
        int seconds = c.get(Calendar.SECOND);
        String curTime = hours + ":" + minutes + ":" + seconds;
        watch.setText(curTime);
    }

    public void onStart() {
        super.onStart();
        Thread background = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    while(ContinueThread.get()) {
                        Thread.sleep(1000);
                        handler.sendMessage(handler.obtainMessage());
                    }
                } catch (Throwable t) {

                }

            }

        });
        ContinueThread.set(true);
        background.start();
    }
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            displayCurrentTime();
        }
    };
    public void onStop() {
        super.onStop();
        ContinueThread.set(false);
    }

}
Это было полезно?

Решение

if you want your code to run in UI thread you might want Handler.

A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue. Each Handler instance is associated with a single thread and that thread's message queue.

When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it -- from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.

There are two main uses for a Handler:

(1) to schedule messages and runnables to be executed as some point in the future;

and

(2) to enqueue an action to be performed on a different thread than your own.

You can read more here about handler and its usage.

For boolean and atomic boolean Read this and this

Другие советы

Not sure whether you have read Handler and AtomicBoolean document. Handler is associated with thread's message queue, which is thread-safe. so you can transfer data between threads by Handler, and not need to worry about synchronization. AtomicBoolean is a class for boolean value synchronization, not a primitive data type.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top