android 处理器crash刊物
日志记录程序是为了方便各种异常情况,为了方便日后的维修方案进行维修,程序无法百分百健康,完美,有必要保存在日志中代码。易于维护。Java了一个接口UncaughtExceptionHandler,Thread.setDefaultUncaughtExceptionHandler(handler)设置当线程因为未捕获到异常而突然终止,而且没有为该线程定义其它处理程序时所调用的默认处理程序。
所以我们能够继承UncaughtExceptionHandler。 在handler实现对日志的读写
public class CrashHandler implements UncaughtExceptionHandler {
// 系统默认的UncaughtException处理
private Thread.UncaughtExceptionHandler mDefaultHandler;
public CrashHandler() {
mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
}
@Override
public void uncaughtException(Thread thread, Throwable ex) {
try {
// 创建日志文件
File file = createCreashLogFile();
// 写入日志文件
if (file != null && file.exists()) {
writeLog(file, ex);
}
} catch (Exception e) {
LogUtils.w("", e);
}
// 将异常抛给系统处�?
mDefaultHandler.uncaughtException(thread, ex);
}
private void writeLog(File logFile, Throwable ex) {
PrintStream printStream = null;
FileOutputStream fos = null;
// 写入日志文件
try {
fos = new FileOutputStream(logFile);
printStream = new PrintStream(fos);
ex.printStackTrace(printStream);
} catch (Exception e) {
LogUtils.w("", e);
} finally {
closeQuietly(printStream);
closeQuietly(fos);
}
}
private void closeQuietly(OutputStream os) {
if (os != null) {
try {
os.close();
} catch (IOException e) {
LogUtils.w("", e);
}
}
}
/** 创建�?个空白的崩溃日志文件 */
public static File createCreashLogFile() throws IOException {
if (!isExternalStorageAvaliable()) { // �?
查存储是否可�?
return null;
}
File directory = new File(Environment.getExternalStorageDirectory()
+ "/ViolationQuery/crash_log");
if (!directory.exists()) {
directory.mkdirs();
}
File file = new File(directory, createCrashLogFileName());
if (file.exists()) {
file.delete();
}
file.createNewFile();
return file;
}
/** 存储是否可用 */
public static boolean isExternalStorageAvaliable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
} else {
return false;
}
}
private static String createCrashLogFileName() {
String dateString = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
return "CrashLog_" + dateString + ".txt";
}
}
<pre name="code" class="java">public class CrashManager {
public static void start() {
// 设置异常处理实例
CrashHandler handler = new CrashHandler();
Thread.setDefaultUncaughtExceptionHandler(handler);
}
}
然后在Application中调用Application中CrashManager.start();这样就大功告成了
版权声明:本文博主原创文章。博客,未经同意不得转载。
android 处理器crash刊物的更多相关文章
- Android程序crash处理
Android程序crash处理 时间 2014-11-24 13:45:37 CSDN博客 原文 http://blog.csdn.net/allen315410/article/details ...
- Android Native crash日志分析
在Android应用crash的类型中,native类型crash应该是比较难的一种了,因为大家接触的少,然后相对也要多转几道工序,所有大部分对这个都比较生疏.虽然相关文章也有很多了,但是我在刚开始学 ...
- Android程序Crash时的异常上报
转载请注明来源:http://blog.csdn.net/singwhatiwanna/article/details/17289479 前言 大家都知道,android应用不可避免的会发生crash ...
- 获取Android崩溃crash信息并写入日志发送邮件
一.实现Thread.UncaughtExceptionHandlerUnChecked异常发生时,由于没有相应的try…catch处理该异常对象,所以Java运行环境将会终止,程序将退出,也就是我们 ...
- android 之 Crash信息的持久化处理
需求: 持久化运行时异常的信息 1.CrashHandler.java import android.content.Context; import android.content.pm.Packag ...
- Android Tombstone/Crash的log分析和定位
有一句话叫做常在河边走,哪有不湿鞋.我们这些研究和开发Android的project师正应了这句话,相必大家在调试的时候常常会遇到这么个东西吧 *** *** *** *** *** *** *** ...
- Android常见Crash原因总结(二)
Android平台程序崩溃大家都应该遇到过,force close和ANR应该是大家遇到较多的. 这里把Android平台程序崩溃的各种类型做一个简述和原因列举. 1.ANR(可见ANR): 发生场景 ...
- Android 常见Crash Log汇总
一.BinderProxy@4479b390 is not valid; is your activity running? 原因分析: 因为使用了AsyncTask 异步线程在线程完成以后的onPo ...
- Android常见Crash类型分析(一)
问题1. java.lang.IllegalStateException: The specified child already has a parent. You must call remo ...
随机推荐
- C#反射机制详解(转)
两个现实中的例子:1.B超:大家体检的时候大概都做过B超吧,B超可以透过肚皮探测到你内脏的生理情况.这是如何做到的呢?B超是B型超声波,它可以透过肚皮通过向你体内发射B型超声波,当超声波遇到内脏壁的时 ...
- Codeforces 549G. Happy Line 馋
非常有趣的贪婪: Let's reformulate the condition in terms of a certain height the towers, which will be on t ...
- 2012Android开发热门资料(110个)
下载中心完整附件下载地址:http://down.51cto.com/data/412494 附件内容部分预览: 专题推荐: Android控:平板电脑HD精品游戏软件合集(共32个) http:// ...
- HDU 1241 :Oil Deposits
Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
- 就这样CSDN账号被人盗了??
和往常一样,来到公司后的第一件事情就是看看自己博客.没想到今天一看,小伙伴惊呆了. 莫名其妙地多了这个多不是神马的博文,还好几篇. 这说明CSDN账号也不怎么安全哦,以后小伙伴们要注意了.
- css中换行的几种方式
1.white-space:normal; 这个只针对中文有效 2.word-break:break-all; 强制换行,针对中文,数字,英文等都有效: 3.word-wrap:break-wo ...
- iOS 通讯录-获取联系人属性
内容均来自关东升老师的ios开发指南 上一篇写了联系人框架的一些必须知道的知识 如今写一下读取联系人数据相关操作 要读取通讯录数据库 须要 创建通讯录对象 查询获取数据(全部或者部分) 获取通讯录某一 ...
- [欧拉回路] hdu 3018 Ant Trip
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3018 Ant Trip Time Limit: 2000/1000 MS (Java/Others) ...
- 教你pomeloclient包libpomelo增加cocos2d-x 3.0工程(Windows、Android、IOS平台)
Windows平台 操作系统:Windows7(64-bit) VS版本号:2013 Cocos2d-x版本号:3.0 project路径:E:\cocos2d-prj\ 1.从github下载lib ...
- NSPredicate的用法
一般来说这种情况还是蛮多的,比如你从文件中读入了一个array1,然后想把程序中的一个array2中符合array1中内容的元素过滤出来. 正 常傻瓜一点就是两个for循环,一个一个进行比较,这样效率 ...