• android的Looper例子


    直接贴代码

    MsgThread.java

    package bb.aa.looperdemo;
    
    import android.os.Handler;
    import android.os.Looper;
    import android.os.Message;
    import android.util.Log;
    
    public class MsgThread extends Thread{
    
        public static final String TAG = "MsgThread";
    
        public Handler _handler = null;
    
        @Override
        public void run() {
            Log.d(TAG, "进入Thread的run");
    
            Looper.prepare();
    
    //        _handler = new Handler(Looper.getMainLooper()){
            _handler = new Handler(Looper.myLooper()){
                @Override
                public void handleMessage(Message msg){
                    Log.d(TAG, "获得了message");
                    super.handleMessage(msg);
                }
            };
            Looper.loop();
        }
    
        public void sendMsg(int what, Object object){
            Message message = _handler.obtainMessage();
            message.what = what;
            message.obj = object;
            _handler.sendMessage(message);
        }
    }

    MainActiivty.java

    package bb.aa.looperdemo;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    
    public class MainActivity extends AppCompatActivity {
    
        private MsgThread _msgThread = null;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            startThread();
    
            Button button = (Button)findViewById(R.id.button);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    _msgThread.sendMsg(123, null);
                }
            });
        }
    
        private void startThread(){
            _msgThread = new MsgThread();
            _msgThread.start();
        }
    }
  • 相关阅读:
    (转帖) oracle是否归档模式及修改模式
    (转帖) Oracle实例恢复(Oracle instance recovery)
    实习第一周总结
    UML类图几种关系的总结
    表格排序
    利用js查找页面中的内链,外链
    事件机制(事件冒泡与事件捕获)
    谈 CSS 模块化
    初学后台框架总结篇二——快速了解CI框架
    初学后台框架总结篇一——学习过程
  • 原文地址:https://www.cnblogs.com/ziyouchutuwenwu/p/5728830.html
Copyright © 2020-2023  润新知