• 安卓当中的线程和每秒刷一次


    package max.test04;

    import android.app.Activity;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Handler.Callback;
    import android.os.Message;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;

    public class MainActivity extends Activity implements Callback {


    private TextView txt1=null;
    private Button btn1=null;
    private Button btn2=null;
    private Handler handler;
    private int i=0;
    private boolean bl;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    txt1=(TextView)this.findViewById(R.id.txt1);
    btn1=(Button)this.findViewById(R.id.btn1);
    btn2=(Button)this.findViewById(R.id.btn2);
    btn2.setOnClickListener(btn2Click);
    handler = new Handler(this);

    btn1.setOnClickListener(new View.OnClickListener()
    {

    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    bl=false;
    }

    });
    }

    private View.OnClickListener btn2Click= new View.OnClickListener() {

    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    new Thread(new TThreadTest()).start();
    }
    };

    @Override
    public boolean handleMessage(Message msg) {
    // TODO Auto-generated method stub
    switch(msg.what)
    {
    case 0:

    String s = (String)msg.obj;
    // //if (msg.obj InstanceOf String)
    txt1.setText(s);
    // Log.d("log text", s);

    break;
    case 1:
    break;
    }

    return false;
    }

    private class TThreadTest implements Runnable
    {

    @Override
    public void run() {
    // TODO Auto-generated method stub
    //etText.setText("线程赋值");
    //handler.obtainMessage(0, "线程赋值").sendToTarget();
    bl=true;

    while(bl){
    i++;
    handler.obtainMessage(0,String.valueOf(i)).sendToTarget();
    try {
    Thread.sleep(1000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }

    }

    }

  • 相关阅读:
    CompareUtil
    linux awk学习笔记
    linux用grep查找包含两个关键字的命令
    mysql 使用set names 解决乱码问题
    对私有静态方法进行单测
    使用JUnit测试预期异常
    Tortoise svn 冲突解决主要办法
    tortoise svn冲突解决
    word-break与word-wrap
    移动端适配
  • 原文地址:https://www.cnblogs.com/hechunming/p/4092602.html
Copyright © 2020-2023  润新知