美文网首页
将异常信息输出到本地文件(备忘)

将异常信息输出到本地文件(备忘)

作者: CalvinNing | 来源:发表于2016-12-06 17:09 被阅读5次
public class MyApplication extends Application {
    public static SimpleDateFormat format;

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {

            @Override
            public void uncaughtException(Thread t, Throwable e) {
                try {
                    StringBuffer currentTime = new StringBuffer().append("\n********************************\n")
                            .append(MyApplication.format.format(new Date()))
                            .append("\n********************************\n");
                    PrintStream ps = new PrintStream(new FileOutputStream(new File(getFilesDir(), "log.txt"), true));
                    ps.print(currentTime);
                    e.printStackTrace(ps);
                } catch (FileNotFoundException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

                // 当有异常的时候自己吧自己杀死 闪退!
                Process.killProcess(Process.myPid());
            }
        });
    }

}

相关文章

网友评论

      本文标题:将异常信息输出到本地文件(备忘)

      本文链接:https://www.haomeiwen.com/subject/eburmttx.html