Source code introduction This project is a simple example of global exception capture. After capturing the exception, the exception information can be written to a file for later analysis or prompted in a friendly manner before exiting the program. Source code running screenshot Source code snippet: - public class UncaughtException implements UncaughtExceptionHandler {
- private final static String TAG = "UncaughtException" ;
- private static UncaughtException mUncaughtException;
- private Context context;
- private DateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd-HH-mm-ss" );
-
- private Map<string, string= "" > infos = new HashMap<string, string= "" >();
- public Context getContext() {
- return context;
- }
-
- public void setContext(Context context) {
- this .context = context;
- }
-
- private UncaughtException() {
-
- }
-
-
-
-
-
-
- public synchronized static UncaughtException getInstance() {
- if (mUncaughtException == null ) {
- mUncaughtException = new UncaughtException();
- }
- return mUncaughtException;
- }
-
-
-
-
- public void init() {
- Thread.setDefaultUncaughtExceptionHandler(mUncaughtException);
- }
-
- @Override
- public void uncaughtException(Thread thread, Throwable ex) {
-
-
- saveCrashInfo2File(ex);
- Log.e(TAG, "uncaughtException thread : " + thread + "||name=" + thread.getName() + "||id=" + thread.getId() + "||exception=" + ex);
-
-
-
-
-
- showDialog() ;
- }
-
- private void showDialog() {
- new Thread() {
- @Override
- public void run() {
- Looper.prepare();
- new AlertDialog.Builder(context).setTitle( "Tears prompt" ).setCancelable( false ).setMessage( "My Lord, I am broken..." )
- .setNeutralButton( "I know" , new OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- System.exit( 0 );
-
- }
- }).create().show();
- Looper.loop();
- }
- }.start();
- }
-
-
-
-
-
-
-
- private String saveCrashInfo2File(Throwable ex) {
- StringBuffer sb = new StringBuffer();
-
- long timestamp = System.currentTimeMillis();
- String time = formatter.format( new Date());
- sb.append( "\n" +time+ "----" );
- for (Map.Entry<string, string= "" > entry : infos.entrySet()) {
- String key = entry.getKey();
- String value = entry.getValue();
- sb.append(key + "=" + value + "\n" );
- }
-
- Writer writer = new StringWriter();
- PrintWriter printWriter = new PrintWriter(writer);
- ex.printStackTrace(printWriter);
- Throwable cause = ex.getCause();
- while (cause != null ) {
- cause.printStackTrace(printWriter);
- cause = cause.getCause();
- }
- printWriter.close();
-
- String result = writer.toString();
- sb.append(result);
- try {
-
- String fileName = "exception.log" ;
-
- if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
- String path = "/sdcard/crash/" ;
- File dir = new File(path);
- if (!dir.exists()) {
- dir.mkdirs();
- }
- FileOutputStream fos = new FileOutputStream(path + fileName, true );
- fos.write(sb.toString().getBytes());
- fos.close();
- }
-
- return fileName;
- } catch (Exception e) {
- Log.e(TAG, "an error occurred while writing file..." , e);
- }
-
- return null ;
- }
- }</string,></string,></string,>
Source code link: http://download..com/data/1980812 |