在主线程中
Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandler(this));
class MyUncaughtExceptionHandler implements UncaughtExceptionHandler{
private final Context myContext;
public MyUncaughtExceptionHandler(Context context){
myContext=context;
}
@Override
public void uncaughtException(Thread thread, Throwable ex) {
// TODO Auto-generated method stub
StringWriter stackTrace=new StringWriter();
ex.printStackTrace(new PrintWriter(stackTrace));
System.err.println(stackTrace);
//
Intent sendIntent=new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"5782652@qq.com"});
sendIntent.putExtra(Intent.EXTRA_TEXT, stackTrace.toString());
sendIntent.putExtra(Intent.EXTRA_SUBJECT,"exception");
sendIntent.setType("message/rfc822");
myContext.startActivity(sendIntent);
android.os.Process.killProcess(android.os.Process.myPid());
}
}