美文网首页
android —— Handler的内存泄漏

android —— Handler的内存泄漏

作者: 未聞椛洺 | 来源:发表于2017-06-28 10:54 被阅读0次

    private class MyHandler extends Handler {

    //弱引用防止内存泄漏

    WeakReferencem Activity;

    public MyHandler(MainActivity activity){

    mActivity=newWeakReference(activity);

    }

    public void handleMessage(android.os.Message msg) {

    MainActivity activity =mActivity.get();

    switch(msg.what) {

    caseERROR:

    Toast.makeText(activity,"网络异常",Toast.LENGTH_SHORT).show();

    break;

    caseERROR_CITY:

    String str = (String) msg.obj;

    urlLog.setText(str);

    Toast.makeText(activity,"检查url是否正确",Toast.LENGTH_SHORT).show();

    break;

    caseSUCCESS:

    String strw = (String) msg.obj;

    urlLog.setText(strw);

    break;

    caseTIMEOK:

    String strTime = (String) msg.obj;

    time.setText(strTime);

    break;

    caseMD5:

    String md = (String) msg.obj;

    urlLog.setText(md);

    break;

    }

    }

    }

    相关文章

      网友评论

          本文标题: android —— Handler的内存泄漏

          本文链接:https://www.haomeiwen.com/subject/xwxucxtx.html