Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發實例 >> Android Thread 介紹與實例

Android Thread 介紹與實例

編輯:Android開發實例

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就不會銷毀,java的機制就是這樣,這是我們推薦的線程機制,下面著重說一下可能遇到的問題

2.同樣是剛剛的例子,我們將Handler定義成static

[java]
public static Handler mHandler = new Handler(){

public static Handler mHandler = new Handler(){此時,在退出應用再重新進入時,由於Handler並不會有新的實例,因此,th_1與th_2同時發消息給一個static Handler 或者說是指向了同一塊內存區域,這時就會出現TextView上的數字來回跳的現象

3.這樣也可以

使用static定義Handler也不是不可以,只要在Activity的onCreate()中重新實例一個Handler,這樣,JVM分配另一塊內存給新的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 {

    public Handler mHandler = null;
    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);
        mHandler = new TestHandler();
        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();

    }
    class TestHandler extends Handler
    {
        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub 
            super.handleMessage(msg);
            System.out.println("Handler running :"+msg.what+"!");
            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 {

    public Handler mHandler = null;
 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);
        mHandler = new TestHandler();
        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();

    }
    class TestHandler extends Handler
    {
  @Override
  public void handleMessage(Message msg) {
   // TODO Auto-generated method stub
   super.handleMessage(msg);
   System.out.println("Handler running :"+msg.what+"!");
   mTextView.setText(String.valueOf(msg.what));
  }

    }
}


當然,總的來說Java還是不推薦使用static變量的,這本身也不符合面向對象的變成思想,所以,建議除了一些final值,盡量還是多使用消息機制來解決問題,維護也輕松些

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved