This handler class should be static or leaks might occur

This handler class should be static or leaks might occur

问题描述

Handler使用方法一文,谈到了Handler的用法,但示例代码中有Warning: this handler class should be static or leaks might occur.

网上有较多的解决办法,这里记录用独立的Handler子类的方法。

public class MyHandler extends Handler

把原来MainActivie中定义的inner class Handler独立出来:

package com.example.handlerexample;

import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

public class MyHandler extends Handler {
    private TextView textView = null;

    public MyHandler(TextView textView) {
        this.textView = textView;
    }
    @Override
    public void handleMessage(Message msg) {
        int count = msg.what;
        textView.setText("" + count);
    }
}

MainActivity中的优化

public class MainActivity extends Activity {

private Handler handler = null;
/*private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        int count = msg.what;
        progress.setText("" + count);
    }
};*/

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_layout);

    start = (Button) findViewById(R.id.start);
    progress = (TextView) findViewById(R.id.progress);
    handler = new MyHandler(progress);
    start.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            calculate();
        }

    });
}

另一种方法

protected static final String TAG = "MainActivity";
private Button start = null;
private TextView progress = null;

private static class AnotherHandler extends Handler {
    WeakReference<MainActivity> activity = null;

    public AnotherHandler(MainActivity activity) {
        this.activity = new WeakReference<MainActivity>(activity); 
    }

    @Override
    public void handleMessage(Message msg) {
        if (activity == null) return;

        int count = msg.what;
        activity.get().getTextView().setText("" + count);
    }
}

private Handler handler = new AnotherHandler(this);
/*private static Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        int count = msg.what;
        progress.setText("" + count);
    }
};*/

private Runnable adding = new Runnable() {

    @Override
    public void run() {
        double d;
        for (int count = 1; count <= 1000; count++) {
            for (int i = 0; i < 1000; i++) {
                d = Math.sqrt(Math.sqrt(i));
                Log.d(TAG, "count = " + count + ", sqrt(sqrt(" + i + "))=" + d);
            }
            if (count % 10 == 0) {
                handler.sendEmptyMessage(count / 100);
            }
        }
    }

};

private void calculate() {
    Thread thread = new Thread(null, adding, "adding");
    thread.start();
}

public TextView getTextView() {
    return progress;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_layout);

    start = (Button) findViewById(R.id.start);
    progress = (TextView) findViewById(R.id.progress);
    //handler = new MyHandler(progress);
    start.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            calculate();
        }

    });
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值