首先我们把MVC的代码写好 进行测试 案例是一个简单的获取图片的demo
public class MainActivity extends Activity implements Callback { private ImageVIEw imageVIEw; private final static String PATH = "https://imgconvert.csdnimg.cn/aHR0cHM6Ly9mb3J1bS5taWFuYmFvYmFulmNul2RhdGEvYXR0YWNobWVudC9mb3J1bS8yMDE4MDMvMjAvMTQwMTUzaTc1NjJleHpqcm5hZ2FqYS5naWY"; private Handler handler = new Handler(new Handler.Callback() { @OverrIDe public boolean handleMessage(Message msg) { switch (msg.what) { case ImageDownloader.SUCCESS: // 成功 imageVIEw.setimageBitmap((Bitmap) msg.obj); break; case ImageDownloader.ERROR: // 失败 Toast.makeText(MainActivity.this, "下载失败", Toast.LENGTH_SHORT).show(); break; } return false; } }); @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); imageVIEw = findVIEwByID(R.ID.iv_image); // 内存泄漏 new Thread(new Runnable() { @OverrIDe public voID run() { SystemClock.sleep(50000); } }).start(); } // 点击事件 public voID getimage(VIEw vIEw) { ImageBean imageBean = new ImageBean(); imageBean.setRequestPath(PATH); new ImageDownloader().down(this, imageBean); } @OverrIDe public voID callback(int resultCode, ImageBean imageBean) { Message message = handler.obtainMessage(resultCode); message.obj = imageBean.getBitmap(); handler.sendMessageDelayed(message, 500); }}
public interface Callback { /** * @param resultCode 请求结果返回标识码 * @param imageBean Model层数据中bitmap对象(用于C层刷新V) */ voID callback(int resultCode, ImageBean imageBean);}
public class ImageBean { // 网络图片地址 private String requestPath; // 结果返回bitmap对象 private Bitmap bitmap; public String getRequestPath() { return requestPath; } public voID setRequestPath(String requestPath) { this.requestPath = requestPath; } public Bitmap getBitmap() { return bitmap; } public voID setBitmap(Bitmap bitmap) { this.bitmap = bitmap; }}
public class ImageDownloader { // 成功 static final int SUCCESS = 200; // 失败 static final int ERROR = 404; public voID down(Callback callback, ImageBean imageBean) { new Thread(new DownLoader(callback, imageBean)).start(); } static final class DownLoader implements Runnable { private final Callback callback; private final ImageBean imageBean; public DownLoader(Callback callback, ImageBean imageBean) { this.callback = callback; this.imageBean = imageBean; } @OverrIDe public voID run() { try { URL url = new URL(imageBean.getRequestPath()); httpURLConnection httpURLConnection = (httpURLConnection) url.openConnection(); httpURLConnection.setConnectTimeout(5000); httpURLConnection.setRequestMethod("GET"); if (httpURLConnection.getResponseCode() == httpURLConnection.http_OK) { inputStream inputStream = httpURLConnection.getinputStream(); Bitmap bitmap = BitmapFactory.decodeStream(inputStream); showUi(SUCCESS, bitmap); } else { showUi(ERROR, null); } } catch (Exception e) { e.printstacktrace(); showUi(ERROR, null); } } private voID showUi(int resultCode, Bitmap bitmap) { if (callback != null) { imageBean.setBitmap(bitmap); callback.callback(resultCode, imageBean); } } }}
在MianActivity中 我们在onCreat()方法中故意写了内存泄漏的代码 ,一旦用户退出了 MainAvtivity 还会持有对象 如下内存分析情况
总结以上是内存溢出为你收集整理的Android为什么会有MVP呢?全部内容,希望文章能够帮你解决Android为什么会有MVP呢?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)