1. 程式人生 > >AndroidStudio中Handler類的內存溢出風險

AndroidStudio中Handler類的內存溢出風險

append sage read @override .text 溢出 con class 外部類

package com.test.king.xmlparser;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private static TextView tvContent;
    private static final int MSG_FINISH=0x0001;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvContent=findViewById(R.id.tv_content);

    }

    //內部類解決可能存在的內存溢出
    /*
    *、本文描述靜態和非靜態內部類的區別是非靜態內部類持有外部類的引用。
2、內部類實例的持有對象的生命周期大於其外部類對象,那麽就有可能導致內存泄露。
比如,要實例化一個超出activity生命周期的內部類對象,
避免使用非靜態的內部類。建議使用靜態內部類並且在內部類中持有外部類的弱引用。
    *
    * */
    //靜態內部類
    private static class MyHandler extends Handler {
        private final WeakReference<MainActivity> mActivity;
        //構造方法
        public MyHandler(MainActivity activity) {
            mActivity = new WeakReference<MainActivity>(activity);//對外部類的弱引用
        }
        @Override
        public void handleMessage(Message msg) {
            MainActivity activity = mActivity.get();
            if (activity != null) {
                switch (msg.what)
                {
                    case MSG_FINISH:
                        List<String> contents=(List<String>)msg.obj;
                        //在主線程中顯示
                        for(String content:contents)
                        {
                            tvContent.append(content+"\n");
                        }
                        break;
                }
            }
        }
    }
    //new Handler對象處理消息,下文有引用
    private final MyHandler mHandler = new MyHandler(this);
    //警告
    /*
    private Handler handler=new Handler()
    {
        @Override
        //重寫handleMessage方法處理消息
        public void handleMessage(Message msg) {
            switch (msg.what)
            {
                case MSG_FINISH:
                    List<String> contents=(List<String>)msg.obj;
                    //在主線程中顯示
                    for(String content:contents)
                    {
                        tvContent.append(content+"\n");
                    }
                    break;
            }

        }
    };*/


    public  void parser(View view) throws IOException, XmlPullParserException {
     //常規方法
     /*
     List<String> contents=getPullParserContent(getResources(),R.xml.words);
     for(String content:contents)
     {
         tvContent.append(content+"\n");
     }*/
     //解析XML可能需要耗費很長的時間,所以這裏單獨做一個子線程
        new Thread()
        {
            @Override
            //重寫run方法
            public void run()
            {
                try {
                    List<String> contents=getPullParserContent(getResources(),R.xml.words);
                    //完成工作,通知主線程
                    Message msg=mHandler.obtainMessage();//也可以new Message
                    msg.what=MSG_FINISH;
                    msg.obj=contents;
                    mHandler.sendMessage(msg);
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (XmlPullParserException e) {
                    e.printStackTrace();
                }
            }
        }.start();

    }
    private List<String> getPullParserContent(Resources res,int id) throws IOException, XmlPullParserException {
        List<String> contents = null;
        String tagName;
        //XmlPullParser
        XmlPullParser parser = res.getXml(id);
        //Pull解析本質是SAX解析
        int eventType = parser.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            switch (eventType) {
                case XmlPullParser.START_DOCUMENT:
                    Log.i("Test", "START_DOCUMENT");
                    contents=new ArrayList<String>();
                    break;
                case XmlPullParser.END_DOCUMENT:
                    break;
                case XmlPullParser.START_TAG:
                    tagName = parser.getName();
                    if (tagName.equals("word")) {
                        String value = parser.getAttributeValue(0);
                        contents.add(value);
                        Log.i("Test", "START_TAG:" + tagName + " " + value);
                    }
                    break;
                case XmlPullParser.END_TAG:
                    tagName = parser.getName();
                    Log.i("Test", "END_TAG:" + tagName);
                    break;
            }
            eventType = parser.next();
        }
        return contents;
    }
}

  

AndroidStudio中Handler類的內存溢出風險