捕获程序退出异常自动发送邮件

在主线程中

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[]{"[email protected]"});
			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());
		}
		
	}

 

你可能感兴趣的:(发送邮件)