当前位置 : 主页 > 编程语言 > java >

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

来源:互联网 收集:自由互联 发布时间:2021-06-28
gistfile1.txt public class LvbudongActivity extends Activity{ private static class MyHandler extends Handler{ //对Activity的弱引用 private final WeakReference mActivity; public MyHandler(HandlerActivity activity){ mActivity = new WeakR
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); }
  
 
网友评论