Android custom processing crashes

Source: Internet
Author: User

Android custom processing crashes

Users who have used Android phones and Android Developers will experience abnormal exit of the program for a long time. Common users will be very annoyed when such a situation occurs, and may even scold the spam software for a lifetime, then uninstall it. So what should developers do when they encounter this situation during development? Of course, you cannot try your program on every Android phone in the world. This is impossible, so the best way is to let the application automatically send the error message to your server when the common user encounters this situation, and then analyze the cause of the exception, is this a very meaningful thing? Refer to: workshop? In fact, when an exception occurs, the entire application has exited. All the activities related to this application have been destroyed, and AlertDialog will not work any more. Therefore, we need to change our thinking, when the program is abnormal, let it jump to another Activity and pass the error message to this Activity. Here, let the user choose whether to send the error message, but some students say, what I want is a dialog box, not a whole interface. It's easy to do. Isn't it enough to make the Activity into a dialog box style? (For how to set a Dialog style Activity, see: Click here)

For custom exception capture, refer to the original article or download my source code. The implementation step is to start a Service at the same time when the main Activity is started, and put the method to jump to the window Activity into the Service, execute this method when an exception is caught.

TestService. java

public class TestService extends Service {private static TestService mInstance = null;@Overridepublic IBinder onBind(Intent 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 of exception Capture: CrashHandler. java

/*** Custom error handling, collecting error information and sending error reports are all completed here. ** @ param ex * @ return true: If the exception information is processed, false is returned. */private boolean handleException (Throwable ex) {if (ex = null) {return false;} new Thread () {@ Overridepublic void run () {Looper. prepare (); TestService. getInstance (). sendError ("error: made by byl"); logoff. loop ();}}. start (); // collect device parameter information collectDeviceInfo (mContext); // Save the log file saveCrashInfo2File (ex); return false ;}


Send an error 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 () {case R. id. OK: Toast. makeText (this, "sent successfully (" + 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 code: http://download.csdn.net/detail/baiyuliang2013/7784163




Zookeeper

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.