开发中陆陆续续遇到很多莫名其妙的问题,其中最让人头疼的就是application crash。application的crash可能由于很多的原因所引起的,有可能是因为数据的问题,或者是异步AsyncTask的运用不合理,等等等等,都可能造成application crash。
也一直在想如何才能知道application crash,想在application crash的时候做点什么,一直也没有找到一个solution。后来在一个偶然的机会用到一个别人的application可以捕获到crash,就想捡到了宝一样。想尽办法去找到这个application的sourcecode,发现原来很简单,几句话就可以做到。
1. 在Activity的onCreate方法里面添加这句话:
Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandler(this));
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler(this));
- setContentView(R.layout.main);
- click2Crash = (Button)findViewById(R.id.clickToCrash);
- click2Crash.setOnClickListener(new Button.OnClickListener(){
- @Override
- public void onClick(View v) {
- String text = "aaa";
- System.out.print(text.charAt(10));
- }});
- }
2. 写一个自己的MyUncaughtExceptionHandler implementes UncaughtExceptionHandler,实现方法uncaughtException。在这个方法里面做任何想做的事情,比如说将造成application crash的原因发送给开发人员。
- public class UncaughtExceptionHandler implements
- java.lang.Thread.UncaughtExceptionHandler {
- private Context con_;
- public UncaughtExceptionHandler(Context con){
- this.con_ = con;
- }
- @Override
- public void uncaughtException(Thread thread, Throwable ex) {
- StringWriter sw = new StringWriter();
- ex.printStackTrace(new PrintWriter(sw));
- System.err.println(sw);
- Intent bugReportIntent = new Intent(con_, BugReportActivity.class);
- bugReportIntent.putExtra(BugReportActivity.exceptionMsg, sw.toString());
- con_.startActivity(bugReportIntent);
- Process.killProcess(Process.myPid());
- System.exit(10);
- }
- }
3. 写一个自己的Activity,告诉用户,你需要这个exception信息,让他Email给开发人员。
- public class BugReportActivity extends Activity {
- public static final String exceptionMsg = "exceptionMsg";
- private TextView reportContent;
- private Button sendMailBtn;
- private Button cancelBtn;
- protected void onCreate(Bundle bundle){
- super.onCreate(bundle);
- Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler(this));
- setContentView(R.layout.bug_report);
- reportContent = (TextView)findViewById(R.id.reportContent);
- sendMailBtn = (Button)findViewById(R.id.sendMail);
- cancelBtn = (Button)findViewById(R.id.cancel);
- String sw = getIntent().getStringExtra(exceptionMsg);
- reportContent.setText(sw);
- initHandler();
- }
- private void initHandler(){
- sendMailBtn.setOnClickListener(new Button.OnClickListener(){
- @Override
- public void onClick(View v) {
- Toast.makeText(v.getContext(), "Email will be sent to our helpdesk.", Toast.LENGTH_LONG).show();
- }
- });
- cancelBtn.setOnClickListener(new Button.OnClickListener(){
- @Override
- public void onClick(View v) {
- finish();
- }
- });
- }
- }
更不错的可以看下2014版的vlc中的关于crash的处理, 用了StackTraceElement[] trace = ex.getStackTrace(); 追踪.