zoukankan      html  css  js  c++  java
  • Handler(消息机制)

    Demo演示

    //通过Handler事件倒计时的一个操作,并判断状态

    public class MainActivity extends AppCompatActivity {
    private TextView mTextView;
    private Button mButton,mButton2;
    private final static int STATE_START = 100;
    private final static int STATE_RUN = 101;
    private final static int STATE_END = 102;
    //创建Handler对象
    private Handler mHandler = new Handler() {
    public void handleMessage(Message msg){
    switch (msg.what){
    case STATE_START:
    mTextView.setText("倒计时开始");
    break;
    case STATE_RUN:
    mTextView.setText(String.valueOf(msg.arg1));
    break;
    case STATE_END:
    mTextView.setText("倒计时结束");
    break;
    }
    }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mTextView = (TextView)findViewById(R.id.textview);
    mButton = (Button)findViewById(R.id.button);
    mButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    MyThread myThread = new MyThread(10);
    myThread.start();
    }
    });
    }
    class MyThread extends Thread{
    int startnum;
    MyThread(int num){
    startnum = num ;

    }

    @Override
    public void run() {
    super.run();
    Message startMsg = mHandler.obtainMessage();
    startMsg.what=STATE_START;
    mHandler.sendMessage(startMsg);
    while (startnum >=0){
    try {
    Thread.sleep(1000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    Message runMsg = mHandler.obtainMessage();
    runMsg.what =STATE_RUN;
    runMsg.arg1 = startnum;
    mHandler.sendMessage(runMsg);
    startnum--;

    }
    Message endMsg = mHandler.obtainMessage();
    endMsg.what = STATE_END;
    mHandler.sendMessage(endMsg);
    }
    }
    }


    //演示post() postAtTime() postdelayed()
    public class NextActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView mTextView;
    private Button button_post, button_postattime, button_postdelayed;
    private Handler mHandler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_next);
    mTextView = (TextView) findViewById(R.id.textview_next);
    button_post = (Button) findViewById(R.id.button_next_post);
    button_postattime = (Button) findViewById(R.id.button_next_postattime);
    button_postdelayed = (Button) findViewById(R.id.button_next_postdelayed);
    button_postdelayed.setOnClickListener(this);
    button_post.setOnClickListener(this);
    button_postattime.setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {
    switch (v.getId()) {
    case R.id.button_next_post:
    mHandler.post(new Runnable() {
    @Override
    public void run() {
    mTextView.setText("立即执行");
    }
    });
    break;
    case R.id.button_next_postattime:
    mHandler.postAtTime(new Runnable() {
    @Override
    public void run() {
    mTextView.setText("指定时间执行");
    }
    },android.os.SystemClock.uptimeMillis() +2000); //PostAtTime的时间使用的是uptimeMillis()不是currentTimeMillis()
    break;
    case R.id.button_next_postdelayed:
    mHandler.postDelayed(new Runnable() {
    @Override
    public void run() {
    mTextView.setText("延迟时间执行");
    }
    }, 2000);
    break;
    }
    }
    }

  • 相关阅读:
    数学 之 hdu 4722
    DP + math 之 Codeforces 126D
    计算几何 之 hdu 1077 poj 1981 O(n^2logn)
    计算几何 之 hdu 1077 poj 1981 O(n^3)
    字典树 之 hdu 1800
    字典树 之 poj 1056
    字典树 之 hdu 4099
    字典树 之 hdu 1247
    解决java.sql.SQLException: Value '0000-00-00' can not be represented as java.sql.Date
    显示Mac壁纸所在路径以及接下来的事情你懂得
  • 原文地址:https://www.cnblogs.com/niupi/p/5512249.html
Copyright © 2011-2022 走看看