一个可以捕获程序崩溃个 然后可以保存本地的方法
2016-06-27 11:07
399 查看
一个可以捕获程序崩溃个 然后可以保存本地的方法
public class MyApplication extends Application { private String DIR = ""; private String NAME = ""; /** * 为了完全退出程序调用方法 MyApplication.getInstance().addActivity(this); * MyApplication.getInstance().exit(); */ private static MyApplication instance_app; // 用于存儲 Activity 的集合,方便統一关闭 private List<Activity> activityList = new LinkedList<Activity>(); public MyApplication() {} @Override public void onCreate() { super.onCreate(); this.DIR = Environment.getExternalStorageDirectory() .getAbsolutePath() + "/yuedaosurvey/log/"; this.NAME = "client_"+getCurrentDateString() + ".txt"; Thread.setDefaultUncaughtExceptionHandler(uncaughtExceptionHandler); } /** * 捕获错误信息的handler */ private Thread.UncaughtExceptionHandler uncaughtExceptionHandler = new Thread.UncaughtExceptionHandler() { @Override public void uncaughtException(Thread thread, Throwable ex) { ex.printStackTrace(); 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); // 捕获异常结束程序 System.out.println("异常关闭Socket!"); PushApplication.getInstance().logout(); YDApplication.getInstance().exit(); } }; /** * 向文件中写入错误信息 * * @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; } // 单例模式获取唯一的MyApplication实例 public static MyApplication getInstance() { if (instance_app == null) { instance_app = new MyApplication(); } return instance_app; } // 添加Activity到容器中 public void addActivity(Activity activity) { activityList.add(activity); } // 遍历所有Activity并finish public void exit() { for (Activity aty_list : activityList) { aty_list.finish(); } System.exit(0); } public void onLowMemory() { super.onLowMemory(); System.gc(); } }
相关文章推荐
- ant编译java 和eclipse编译的差别
- 黑马程序员-JAVA基础面试-获取10个1-20 的随机数,要求获取的随机数不能相同!
- Linux学习-VIM操作
- linux内核函数 ioremap()的原理及意义
- 二级列表
- fir.im Weekly - 2016 年 Android 最佳实践列表
- jquery广告滚动框
- 电子选频率特性
- poj1840
- 常用的正则表达式大全
- 通用的sql语句
- Yixiaohan---005题,更改目录下所有图片的分辨率
- dm6446 VLIB函数
- git命令大全
- linux使用实战--基础应用篇
- 了解连接池
- 更改VS默认新建文件格式及内容
- 60. Permutation Sequence
- python3.x执行post请求时报错“POST data should be bytes or an iterable of bytes...”的解决方法
- cocos2d lua 上下翻牌