android 记录崩溃日志

jopen 10年前

摘要 在使用自己开发的android应用时,偶尔会出现 系统已停止运行 错误.这时候如果能记录错误日志,是非常有帮助的.

 

    每个android应用都是由一个Application和多个activity或者server构成.应用启动时,会首先启动Application.在Application的onCreate方法中调用

Thread.setDefaultUncaughtExceptionHandler(uncaughtExceptionHandler);

就可以捕获导致应用崩溃的错误信息了.

首先要自定义一个Application,并在AndroidManifest.xml中使用这个Application

<application          android:name=".MyApplication">          ...  </application>

public class MyApplication extends Application {   public static final String DIR = Environment.getExternalStorageDirectory()     .getAbsolutePath() + "/survey/log/";   public static final String NAME = getCurrentDateString() + ".txt";     @Override   public void onCreate() {    super.onCreate();    Thread.setDefaultUncaughtExceptionHandler(uncaughtExceptionHandler);   }     /**    * 捕获错误信息的handler    */   private UncaughtExceptionHandler uncaughtExceptionHandler = new UncaughtExceptionHandler() {      @Override    public void uncaughtException(Thread thread, Throwable ex) {     LogUtil.showLog("我崩溃了");     String info = null;     ByteArrayOutputStream baos = null;     PrintStream printStream = null;     try {      baos = new ByteArrayOutputStream();      printStream = new PrintStream(baos);      ex.printStackTrace(printStream);      byte[] data = baos.toByteArray();      info = new String(data);      data = null;     } catch (Exception e) {      e.printStackTrace();     } finally {      try {       if (printStream != null) {        printStream.close();       }       if (baos != null) {        baos.close();       }      } catch (Exception e) {       e.printStackTrace();      }     }     writeErrorLog(info);     Intent intent = new Intent(getApplicationContext(),       CollapseActivity.class);     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);     startActivity(intent);    }   };     /**    * 向文件中写入错误信息    *     * @param info    */   protected void writeErrorLog(String info) {    File dir = new File(DIR);    if (!dir.exists()) {     dir.mkdirs();    }    File file = new File(dir, NAME);    try {     FileOutputStream fileOutputStream = new FileOutputStream(file, true);     fileOutputStream.write(info.getBytes());     fileOutputStream.close();    } catch (FileNotFoundException e) {     e.printStackTrace();    } catch (IOException e) {     e.printStackTrace();    }     }     /**    * 获取当前日期    *     * @return    */   private static String getCurrentDateString() {    String result = null;    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd",      Locale.getDefault());    Date nowDate = new Date();    result = sdf.format(nowDate);    return result;   }  }

系统错误后要还是要提示用户系统错误.这个是崩溃activity

public class CollapseActivity extends Activity {   private Button btnRestart, btnExit;     @Override   protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.collapse_activity);    AppManage.getInstance().addActivity(this);    btnRestart = (Button) findViewById(R.id.collapse_restart);    btnExit = (Button) findViewById(R.id.collapse_exit);    btnRestart.setOnClickListener(new OnClickListener() {       @Override     public void onClick(View v) {      Intent intent = new Intent(getApplicationContext(),        MainActivity.class);      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);      startActivity(intent);     }    });    btnExit.setOnClickListener(new OnClickListener() {       @Override     public void onClick(View v) {      AppManage.getInstance().exit();     }    });   }  }