Android线程销毁

Hanlder是线程与Activity通信的桥梁,利用handler接收到任务线程,放到任务队列里面派对执行。

//调用该任务线程的run() 方法执行任务线程。

Handler updateBarHandler =new handler();
handler.post(Runnable Thread);

//移除handler里的任务线程,调用线程的stop()方法,销毁线程。

handler.removecallbacks(Runnable Thread);

一个简单的例子如下:

import Android.app.Activity; 
import Android.os.Bundle; 
import Android.os.Handler; 
import Android.util.Log; 
public class HandlerThread extends Activity { 
    private static final String TAG = "HandlerThread"; 
    private Handler mHandler =  new Handler(); 

    private Runnable mRunnable = new Runnable() { 
        public void run() { 
            Log.e(TAG, Thread.currentThread().getName() + "Thread run");         
        } 

    }; 
    @Override
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main);  
        //通过Handler启动线程 
        mHandler.post(mRunnable); 
    } 

    @Override
    protected void onDestroy() { 
        //将线程销毁掉 
        mHandler.removeCallbacks(mRunnable); 
        super.onDestroy(); 
    } 
}

你可能感兴趣的:(Android线程销毁)