close

 

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <Button android:id="@+id/btn_run"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:text="Run"
            android:textAllCaps="false"
            />
    <Button android:id="@+id/btn_pause"
        android:layout_below="@id/btn_run"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="Pause"
        android:textAllCaps="false"
        />
    <TextView
        android:id="@+id/txt_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textColor="@color/white"
        android:background="@color/black"
        android:text="counter"
        android:gravity="center"
        android:textSize="30dp"/>
 
 
</RelativeLayout>

 

 

Runnable(Observable): In the override method run(), it is continuously doing a while loop. 
In the loop, there is a handler called mHandler. It is responsible to send message to its observers.

mCallback(Observer): It is also a worker, which will be notified by the mHandler.
mCallback
has a handleMessage subroutine, which defines what should be done in the job.

Now we're going to create another thread to run a runnable, ie. mRunnable

 


public class MyCounter{
        private boolean mRunning = false;
        private boolean mPause = false;
        private Thread mThread;
        private long cnt = 0;
        private Runnable mRunnable;
        private Handler mHandler;
        private Handler.Callback mCallback;
        private final int mKey = 1;
 
        public MyCounter()
        {
            mRunnable = new Runnable() {
                @Override
                public void run() {
                    do {
                        try {
                            Thread.sleep(1);
                            Message msg = new Message();
                            msg.what = mKey;
                            if(!mPause)
                            {
                                mHandler.sendMessage(msg);
                            }
 
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }while(mRunning);
                }
            };
            mCallback = new Handler.Callback() {
                @Override
                public boolean handleMessage(@NonNull Message msg) {
                    switch (msg.what)
                    {
                        case mKey:
                            cnt++;
                            mTxt.setText("cnt:"+cnt);
                            break;
                        default:
                            break;
                    }
                    return false;
                }
            };
            mHandler = new Handler(mCallback);
        };
        public void Run()
        {
            mThread = new Thread(mRunnable);
            mRunning = true;
mThread = new Thread(mRunnable);
mRunning = true;
mThread.start();
            mThread.start();
        }
        public void Pause()
        {
            mPause = true;
        }
        public void Resume()
        {
            mPause = false;
        }
 
    }

References:

1. Android:使用Handler與Thread更新UI

2. https://developer.android.com/reference/android/os/Handler

3. How to get the current time in YYYY-MM-DD HH:MI:Sec.Millisecond format in Java

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 me1237guy 的頭像
    me1237guy

    天天向上

    me1237guy 發表在 痞客邦 留言(0) 人氣()