博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用handler倒计时
阅读量:5127 次
发布时间:2019-06-13

本文共 5745 字,大约阅读时间需要 19 分钟。

 点击button暂停

public class MainActivity extends AppCompatActivity {    @BindView(R.id.button)    Button button;    @BindView(R.id.first_textview)    TextView textView;    Handler mHandler;    volatile boolean flag=true;    Object object=new Object();    MThread mThread;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        ButterKnife.bind(this);        textView.setText(String.valueOf(1));        mHandler=new Handler(){            @Override            public void handleMessage(Message msg) {                super.handleMessage(msg);                textView.setText(String.valueOf(msg.what));            }        };        mThread=new MThread();        mThread.start();        button.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                if(!mThread.isAlive()){                    mThread=new MThread();                    mThread.start();                    return;                }                if(flag==true){                    flag=!flag;                }                else{                    synchronized (object){                        flag=!flag;                        object.notify();                    }                }            }        });    }    public class MThread extends Thread{        @Override        public void run() {            System.out.println("enter thread");            for(int i=20;i>=0;i--){                if(flag==false){                    synchronized (object) {                        try {                            object.wait();                        } catch (InterruptedException e) {                            e.printStackTrace();                        }                    }                }                System.out.println(i);                mHandler.sendEmptyMessage(i);                try {                    sleep(500);                } catch (InterruptedException e) {                    e.printStackTrace();                }            }        }    }}
View Code

 

 

两个handler:

public class MainActivity extends AppCompatActivity {    @BindView(R.id.button)    Button button;    @BindView(R.id.textview)    TextView textView;    Handler mFirstHandler;    Handler mSecondHandler;    volatile boolean flag = true;    Object object = new Object();    SecondThread mThread;    FirstThread mFirstThread;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity);        ButterKnife.bind(this);        textView.setText(String.valueOf(1));        mSecondHandler = new Handler(new Handler.Callback() {            @Override            public boolean handleMessage(Message msg) {                textView.setText(String.valueOf(msg.what));                return true;            }        });        mThread = new SecondThread();        mThread.start();        mFirstThread = new FirstThread();        mFirstThread.start();    }    public class SecondThread extends Thread {        @Override        public void run() {            Looper.prepare();            Looper.loop();            System.out.println("*****************");            mFirstHandler = new Handler() {                @Override                public void handleMessage(Message msg) {                    mSecondHandler.sendEmptyMessage(msg.what);                }            };        }    }    public class FirstThread extends Thread {        @Override        public void run() {            System.out.println("enter first thread");            for (int i = 20; i >= 0; i--) {                Message message = Message.obtain();                message.what = i;                try {                    Thread.sleep(500);                } catch (InterruptedException e) {                    e.printStackTrace();                }                mFirstHandler.sendMessage(message);            }        }    }}
View Code

 

使用ThreadHandler

public class MainActivity extends AppCompatActivity {    @BindView(R.id.button)    Button button;    @BindView(R.id.textview)    TextView textView;    Handler mFirstHandler;    Handler mSecondHandler;        Thread t=new FirstThread();    volatile boolean flag = true;    Object object = new Object();    HandlerThread handlerThread;        @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity);        ButterKnife.bind(this);        textView.setText(String.valueOf(1));        mSecondHandler = new Handler(new Handler.Callback() {            @Override            public boolean handleMessage(Message msg) {                textView.setText(String.valueOf(msg.what));                return true;            }        });                handlerThread=new HandlerThread("1");        handlerThread.start();        mFirstHandler=new Handler(handlerThread.getLooper()){            @Override            public void handleMessage(Message msg) {                mSecondHandler.sendEmptyMessage(msg.what);            }        };        t.start();    }    public class FirstThread extends Thread {        @Override        public void run() {            System.out.println("enter first thread");            for (int i = 20; i >= 0; i--) {                Message message = Message.obtain();                message.what = i;                try {                    Thread.sleep(500);                } catch (InterruptedException e) {                    e.printStackTrace();                }                mFirstHandler.sendMessage(message);            }        }    }}
View Code

 

转载于:https://www.cnblogs.com/cnblogs321114287/p/9062608.html

你可能感兴趣的文章
腾讯元对象存储之文件删除
查看>>
jdk环境变量配置
查看>>
安装 Express
查看>>
包含列的索引:SQL Server索引的阶梯级别5
查看>>
myeclipse插件安装
查看>>
浙江省第十二届省赛 Beauty of Array(思维题)
查看>>
NOIP2013 提高组 Day1
查看>>
cocos2dx 3.x simpleAudioEngine 长音效被众多短音效打断问题
查看>>
存储(硬件方面的一些基本术语)
查看>>
观察者模式
查看>>
Weka中数据挖掘与机器学习系列之基本概念(三)
查看>>
Win磁盘MBR转换为GUID
查看>>
大家在做.NET B/S项目的时候多用什么设技术啊?
查看>>
Java SE和Java EE应用的性能调优
查看>>
Android设计模式系列--原型模式
查看>>
免费的论文查重网站
查看>>
C语言程序第一次作业
查看>>
leetcode-Sort List
查看>>
中文词频统计
查看>>
了解node.js
查看>>