参考博客:作者:又宠又烦的Android女
官方方法
private Handler mHandler2 = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
return false;
}
});
使用弱引用
static class MyHandler extends Handler {
WeakReference<OuterClass> outerClass;
MyHandler(OuterClass activity) {
outerClass = new WeakReference<OuterClass>(activity);
}
@Override
public void handleMessage(android.os.Message msg) {
OuterClass theClass = outerClass.get();
switch (msg.what) {
case 0: {
break;
}
default: {
Log.w(TAG, "未知的Handler Message:" + msg.what);
}
}
}
}