handler 静态内部类和弱引用结合

gistfile1.txt
public class LvbudongActivity extends Activity{
    


private static class MyHandler extends Handler{
 
        //对Activity的弱引用
        private final WeakReference
 
   mActivity;
 
        public MyHandler(HandlerActivity activity){
            mActivity = new WeakReference
  
   (activity); } @Override public void handleMessage(Message msg) { HandlerActivity activity = mActivity.get(); if(activity==null){ super.handleMessage(msg); return; } switch (msg.what) { case DOWNLOAD_FAILED: Toast.makeText(activity, "下载失败", Toast.LENGTH_SHORT).show(); break; case DOWNLOAD_SUCCESS: Toast.makeText(activity, "下载成功", Toast.LENGTH_SHORT).show(); Bitmap bitmap = (Bitmap) msg.obj; activity.imageView.setVisibility(View.VISIBLE); activity.imageView.setImageBitmap(bitmap); break; default: super.handleMessage(msg); break; } } } private final MyHandler mHandler = new MyHandler(this); }
  
 
相关代码

相关文章

相关标签/搜索