本文主要实现了handler+Thread通信的方式,实现了多线程之间的通信,期间为了避免内存泄漏,在ondestory方法中对线程中的方法进行了终止了处理。然后对直接new Handler会出现This Handler should be static or leaks might occor的警告信息进行了处理,采用静态内部类+弱引用的方式进行了改进。实现如下:
package com.example.androidreview.multithread;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;
import com.example.androidreview.R;
import java.lang.ref.WeakReference;
public class MultiThreadActivity extends Activity {
private MyHandler mHandler = new MyHandler(this);
private TextView mHandlerText = null;
private static final String TAG = "MultiThreadActivityTAG";
private Thread mThread = null;
private volatile boolean isDestroy = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedIn