zoukankan      html  css  js  c++  java
  • android UI跨线程操作

     android应用程序也是单线程程序,主线程为UI线程。

     android 线程是非安全的,即不要在子线程中更新 UI。

    • public class MasterActivity extends Activity {  
    •     TextView tv = null;  
    •     Button btn = null;  
    •       
    •     Handler mHandler = new Handler() {  
    •         @Override  
    •         public void handleMessage(Message msg) {  
    •             if(msg.what == 1) {  
    •                 tv.setText("update UI is success!");  
    •                 btn.setText("update UI is success!");  
    •             }  
    •             super.handleMessage(msg);  
    •         }  
    •     };  
    •       
    •     @Override  
    •     public void onCreate(Bundle savedInstanceState) {  
    •         super.onCreate(savedInstanceState);  
    •         setContentView(R.layout.main);  
    •         System.out.println(Thread.currentThread().getName() + ": " + Thread.currentThread().getId());  
    •         tv = (TextView)findViewById(R.id.text);  
    •         btn = (Button)findViewById(R.id.btn);  
    •         btn.setOnClickListener(new OnClickListener() {  
    •               
    •             @Override  
    •             public void onClick(View v) {  
    •                 Thread thread = new Thread(new Runnable() {  
    •                       
    •                     @Override  
    •                     public void run() {  
    •                         System.out.println(Thread.currentThread().getName() + ": " + Thread.currentThread().getId());  
    •                         Message msg = mHandler.obtainMessage();  
    •                         msg.what = 1;  
    •                         msg.sendToTarget();  
    •                     }});  
    •                 thread.start();  
    •             }  
    •         });  
  • 相关阅读:
    Ehcache缓存配置
    spring3使用task:annotation-driven开始定时
    Constructor >> @Autowired >> @PostConstruct
    面试转载
    阿里面试:MYSQL的引擎区别
    Redis的主从复制的原理介绍
    微服务的调用链
    java的零拷贝机制
    存储过程与触发器面试
    ABA问题
  • 原文地址:https://www.cnblogs.com/fx2008/p/3140642.html
Copyright © 2011-2022 走看看