当前位置:操作系统 > 安卓/Android >>

Android Thread

Android中很重要的一个机制就是线程+消息,当然线程并不是android独有的,下面,简单的说说使用线程的时候应该注意的地方

我们采用最简单的方法来建立一个android的线程+消息的例子

1.Thread + Handler


[java] package com.example.test_thread; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.Message; 
import android.widget.TextView; 
 
public class MainActivity extends Activity { 
 
    TextView mTextView = null; 
//  static TextView mTextView = null;  
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
        mTextView = (TextView)findViewById(R.id.textview); 
        Thread th = new Thread(new Runnable() { 
             
            @Override 
            public void run() { 
                // TODO Auto-generated method stub  
                for(int i = 0;i<1000;i++) 
                { 
                    try { 
                        Thread.sleep(500); 
                        System.out.println("Thread running :"+i+"!"); 
                        Message msg = new Message(); 
                        msg.what = i; 
                        mHandler.sendMessage(msg); 
                    } catch (InterruptedException e) { 
                        // TODO Auto-generated catch block  
                        e.printStackTrace(); 
                    } 
                } 
            } 
        }); 
        th.start(); 
         
    } 
    public Handler mHandler = new Handler(){ 
//    public static Handler mHandler = new Handler(){  
 
        @Override 
        public void handleMessage(Message msg) { 
            // TODO Auto-generated method stub  
            super.handleMessage(msg); 
             
            mTextView.setText(String.valueOf(msg.what)); 
        } 
         
    }; 
     

package com.example.test_thread;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

public class MainActivity extends Activity {

 TextView mTextView = null;
// static TextView mTextView = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextView = (TextView)findViewById(R.id.textview);
        Thread th = new Thread(new Runnable() {
   
   @Override
   public void run() {
    // TODO Auto-generated method stub
    for(int i = 0;i<1000;i++)
    {
     try {
      Thread.sleep(500);
      System.out.println("Thread running :"+i+"!");
      Message msg = new Message();
      msg.what = i;
      mHandler.sendMessage(msg);
     } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
    }
   }
  });
        th.start();
       
    }
    public Handler mHandler = new Handler(){
//    public static Handler mHandler = new Handler(){

  @Override
  public void handleMessage(Message msg) {
   // TODO Auto-generated method stub
   super.handleMessage(msg);
   
   mTextView.setText(String.valueOf(msg.what));
  }
     
    };
   
}
当我们用以上方式建立线程时,进入应用之后,线程开始运行,Handler接收消息改变UI中的TextView,此时一切正常

当按下退出时,程序退出,但是程序进程还在stack中,因此主线程之子线程,也就是我们定义的th(th_1)不会退出,此时,在log信息中可以看到,system.out还在print数字

当再次进入程序的时候,可以看到,log中打印的信息double,但是UI会按照新线程(th_2)的次序改变

此时th_1仍在运行,th_1使用的 handler_1也在运行,只不过上一个Activity的状态已经是finish,因此不会改变UI  this ->mFinished= true

其实只要th_1中有关于上一个Activity的引用,那么Activity就

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,