在Android Activity中捕获Application Crash

因为接触android开发的时间并不久,对于android系统、机制的理解也是只知其一;不知其二。因此在开发中陆陆续续遇到不少莫名其妙的问题,其中最让人头疼的就是application crash。application的crash可能因为不少的缘由所引发的,有多是由于数据的问题,或者是异步AsyncTask的运用不合理,等等等等,均可能形成application crash。 java

也一直在想如何才能知道application crash,想在application crash的时候作点什么,一直也没有找到一个solution。后来在一个偶然的机会用到一个别人的application能够捕获到crash,就想捡到了宝同样。想尽办法去找到这个application的sourcecode,发现原来很简单,几句话就能够作到。 android

 

1. 在Activity的onCreate方法里面添加这句话: app

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的缘由发送给开发人员。  ide

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给开发人员。 this

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();
}
});
}
}

google

一直都是看别人的blog学习东西的,今天第一次发个blog。有什么很差的地方请你们见谅。:-) spa

 

发现一个GoogleProject专门关注这个Application Crash Report for Android的,简称ACRA.你们能够在下面这个连接上面找到相关内容。 code

http://code.google.com/p/acra/