android 記錄崩潰日志
摘要 在使用自己開發的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(); } }); } }
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!