Memory overflow risk of handler class in Androidstudio

Source: Internet
Author: User

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); }//Inner class resolves possible memory overflow/*, the difference between static and non-static inner classes described in this document is that non-static inner classes hold references to external classes. 2. An internal class instance holds an object whose lifetime is greater than its outer class object, which can lead to memory leaks. For example, to instantiate a more than actIvity the inner class object of the life cycle, avoiding the use of non-static inner classes.    It is recommended that you use static inner classes and hold weak references to external classes within the inner class. * * *//static internal class private static class MyHandler extends Handler {private final Weakreference<mainactivit        Y> mactivity; Construction method Public MyHandler (mainactivity activity) {mactivity = new weakreference<mainactivity> (activ  ity);//Weak reference to external class} @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; Display for (String content:contents) {Tvconten in the main thread                        T.append (content+ "\ n");                } break; }}}}//new handler object processing message, the following is a reference to private final MyHandler MhandleR = new MyHandler (this); Warning/* Private Handler handler=new Handler () {@Override//override Handlemessage method handle message public Voi                    D handlemessage (Message msg) {switch (msg.what) {case Msg_finish:                    List<string> contents= (list<string>) msg.obj; Display for (String content:contents) {tvcontent.append (con) in the main thread                    Tent+ "\ n");            } break; }}};*/public void parser (view view) throws IOException, xmlpullparserexception {//General method/* Li     St<string> contents=getpullparsercontent (Getresources (), r.xml.words);     for (String content:contents) {tvcontent.append (content+ "\ n");            }*///parsing XML can take a long time, so here's a single child thread new Thread () {@Override//rewrite the Run method    public void Run () {try {                List<string> contents=getpullparsercontent (Getresources (), r.xml.words); Complete the work, notify the main thread Message msg=mhandler.obtainmessage ();//You can also new Message Msg.what=msg_fini                    SH;                    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 parsing essence is sax parsing 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; }}

  

Memory overflow risk of handler class in Androidstudio

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.