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());
}
});
}
}
网友评论