Android phone users and Android developers will encounter the program unexpectedly quit the situation, ordinary users encounter this situation, it must be very annoying, and even scold a lifetime of garbage software, and then uninstall. So how do developers encounter this situation in the development process, of course, you can not have every Android phone in the world you have to try your program is not unusual, this is impossible to do, so the best way is, when ordinary users encounter this situation, let the application automatically send the error message to your server, Then you have to analyze the cause of the anomaly, is this a very meaningful thing ah? This article refers to from: http://blog.csdn.net/liuhe688/article/details/6584143, see some small partners, some of the needs of the abnormal exit, pop up a dialog box, let the user choose whether to send the error message, This is a good idea, some children's shoes may immediately think of Alertdialog, but the actual situation, there is no response, Alertdialog did not play out, this is why? In fact, when the whole application has exited, and all the activity related to this application has been destroyed, alertdialog of course will not work, so we have to change a way of thinking, when the program is abnormal, let it jump to another activity, and the error message to the activity, and here to let the user choose whether to send the error message, but some students say, I want a dialog box, not a whole interface, this is good to do, the activity into a dialog box style is not OK? (How to Set dialog-style activity, see: click here)
Custom exception Capture please look at the original, or download my source code, the implementation step is to start the main activity at the same time to start a service, the jump to the windowing activity method into the service, when caught in the exception, the implementation of this method.
Testservice.java
public class Testservice extends Service {private static testservice minstance = null; @Overridepublic ibinder Onbind (inten T arg0) {return null;} @Overridepublic void OnCreate () {super.oncreate (); minstance = this;} public static Testservice getinstance () {return minstance;} Public void Senderror (final String message) {Intent Intent = new Intent (this, senderroractivity.class); Intent.setflags (Intent.flag_activity_new_task); Intent.putextra ("MSG", message); StartActivity (intent); Stopself () ;}}
Handling for exception capture: Crashhandler.java
/** * Custom error handling, collect error messages to send error reports, etc. are done here. * * @param ex * @return true: Returns False if the exception information is processed; */private boolean handleexception (Throwable ex) {if (ex = = null) {R Eturn false;} New Thread () {@Overridepublic void run () {looper.prepare (); Testservice.getinstance (). Senderror ("Error:made by Byl"); Looper.loop (); }}.start ();//Collect device parameter information collectdeviceinfo (mcontext);//Save log file Savecrashinfo2file (ex); return false;}
Send the wrong activity:
public class Senderroractivity extends Activity implements onclicklistener{private Button ok,cancel;private String Error _msg; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_loginoutinfo); GetWindow (). setlayout (Layoutparams.match_parent, layoutparams.match_parent); ok= (Button) Findviewbyid (R.id.ok); cancel= (Button) Findviewbyid (r.id.cancel); error_msg =getintent (). Getstringextra ("msg"); Ok.setonclicklistener (this); Cancel.setonclicklistener (this);} @Overridepublic void OnClick (View v) {switch (V.getid ()) {R.id.ok:toast.maketext (this, "Send succeeded (" +error_msg+ "), 1). Show (); Finish (); Break;case r.id.cancel:finish (); break;default:break;}} @Overridepublic boolean onKeyDown (int keycode, keyevent event) {if (keycode = = Keyevent.keycode_back) {return true;} Return Super.onkeydown (KeyCode, event);}}
Source: http://download.csdn.net/detail/baiyuliang2013/7784163