1 Android Note exception capture

Source: Internet
Author: User
<span id="Label3"></p><p><p>Catch an exception when the program crashes</p></p><p><p>The step first calls Thread.setdefaultuncaughtexceptionhandler (new Defaultexceptionhandler (oncreat) in the Activity's This.getapplicationcontext (), crashinterface));<br>Implementing a subclass that inherits Uncaughtexceptionhandler</p></p><pre><span style="color: #008080;"><span style="color: #008080;">1</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Defaultexceptionhandler<span style="color: #0000ff;"><span style="color: #0000ff;">Implements</span></span><span style="color: #000000;"><span style="color: #000000;">Uncaughtexceptionhandler {</span></span><span style="color: #008080;"><span style="color: #008080;">2</span></span> <span style="color: #008080;"><span style="color: #008080;">3</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span>Context act =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #008080;"><span style="color: #008080;">4</span></span> <span style="color: #000000;"><span style="color: #000000;">crashinterface crashinterface;</span></span><span style="color: #008080;"><span style="color: #008080;">5</span></span> <span style="color: #000000;"><span style="color: #000000;">@Override</span></span><span style="color: #008080;"><span style="color: #008080;">6</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">uncaughtexception (Thread arg0, throwable arg1) {</span></span>Intent sintent=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Intent (act, Crashactivity.<span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #ff0000;"><span style="color: #ff0000;">sintent.addflags (intent.flag_activity_new_task);//must be brought</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">act.startactivity (sintent);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> in</span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Crashinterface.finsh ();</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>Exception here<span style="color: #008080;"><span style="color: #008080;"></span> to</span> <span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">;</span></span><span style="color: #008080;"><span style="color: #008080;"></span> +</span> <span style="color: #000000;"><span style="color: #000000;"> }</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #008080;"><span style="color: #008080;"></span> the</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">Defaultexceptionhandler (Context act,crashinterface crashinterface) {</span></span><span style="color: #008080;"><span style="color: #008080;"></span> *</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>. crashinterface=<span style="color: #000000;"><span style="color: #000000;">crashinterface;</span></span><span style="color: #008080;"><span style="color: #008080;"></span> $</span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>. act =<span style="color: #000000;"><span style="color: #000000;">act; </span></span><span style="color: #008080;"><span style="color: #008080;">Panax Notoginseng</span></span> <span style="color: #000000;"><span style="color: #000000;"> } </span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>}</pre><p><p>In the activity that needs to catch the exception oncreat</p></p><pre> <span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onCreate (Bundle Savedinstancestate) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">. OnCreate (savedinstancestate); View</span></span>= Getlayoutinflater (). Inflate (r.layout.activity_main,<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">); View.setsystemuivisibility (view.system_ui_flag_hide_navigation</span></span>|<span style="color: #000000;"><span style="color: #000000;">view.system_ui_flag_immersive_sticky); Setcontentview (view); <span style="color: #ff0000;">crashinterface crashinterface</span> </span></span><span style="color: #ff0000;"><span style="color: #ff0000;">=new crashinterface () {@Override public void Finsh () {//TODO auto- Generated method stub finish (); Process.killprocess (process.mypid ()); } }; Thread.setdefaultuncaughtexceptionhandler (new Defaultexceptionhandler (</span> this</span><span style="color: #000000;"><span style="color: #000000;"> <span style="color: #ff0000;">. Getapplicationcontext (), crashinterface));</span> }</span></span></pre><p><p></p></p><p><p>1 Android Note exception capture</p></p></span>

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.