• 使用handler倒计时


     点击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
  • 相关阅读:
    各种数据库的连接
    orcle form 传数据乱码
    屏蔽防止被别的网站嵌入框架代码(防止被人frame)
    jQuery中通过$.browser来判断浏览器
    jQuery浏览器类型判断和分辨率判断
    jQuery的一些常用的方法(转载)
    当Table中td内容为空时,显示边框的办法
    jQuery下拉框扩展和美化插件Chosen
    jQuery美化下拉菜单插件dropkick
    jQuery模仿人类打字效果插件typetype
  • 原文地址:https://www.cnblogs.com/cnblogs321114287/p/9062608.html
Copyright © 2020-2023  润新知