Android中防止程序崩溃,以及拿到crash日志分析定位

1.添加该工具类方法

public class CrashProtectManager {    private static CrashProtectManager mInstance;    private static Context mContext;    private CrashProtectManager() {    }    public static CrashProtectManager getInstance(Context context) {        if (mInstance == null) {            mContext = context.getApplicationContext();            mInstance = new CrashProtectManager();        }        return mInstance;    }    public void init() {        //crach 防护        Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {            @Override            public void uncaughtException(Thread t, Throwable e) {                handleFileException(e);                if (t == Looper.getMainLooper().getThread()) {                    handleMainThread(e);                }            }        });    }    //日志文件系统    private void handleFileException(Throwable e) {        //通过Throwable 生成字符串        Writer writer = new StringWriter();        PrintWriter printWriter = new PrintWriter(writer);        e.printStackTrace(printWriter);        printWriter.close();        String result = writer.toString();        //定义文件名        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");        String time = dateFormat.format(new Date());        String fileName = "crash-" + time + ".txt";        try {            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {                File cacheDir = mContext.getCacheDir();                if (!cacheDir.exists()) {                    cacheDir.mkdirs();                }                File cacheFile = new File(cacheDir.getAbsolutePath(), fileName);                if (!cacheDir.exists()) {                    cacheFile.createNewFile();                }                //把字符串写入到文件                FileOutputStream outputStream = new FileOutputStream(cacheFile);                outputStream.write(result.getBytes());                outputStream.close();            }        } catch (Exception e1) {            e1.printStackTrace();        }    }    private void handleMainThread(Throwable e) {        while (true) {            try {                Looper.loop();            } catch (Throwable e1) {                handleFileException(e1);            }        }    }}

2.在application中的onCreate()初始化

CrashProtectManager.getInstance(this).init();

3.日志生成的文件在

data/data/packagename/cahe  目录下

 

更多相关文章

  1. Android 制定的ROM包(文件系统根目录结构分析)
  2. Android中xml文件读写
  3. Android中Manifest.xml配置文件
  4. android之layout配置文件解读
  5. android 从tomcat读取文件出错:connect failed: ECONNREFUSED
  6. Android:dimen尺寸资源文件的使用
  7. Android中五中布局文件的使用和介绍
  8. android 读取assets指定文件

随机推荐

  1. cmake 编译 ogre3D(version1.9) for androi
  2. Android 与 JS 交互数据上限问题【Refusi
  3. 活用Android的Message Queue(1/3)
  4. 面试例题4:绘制5行文本,每一行的字体大小逐
  5. eclipse中无法新建Android工程 出现问题:P
  6. Android之Camera的简单实例
  7. 【移动开发】Android中WIFI开发总结(二)
  8. android 7.1.1 软件升级安装报解析软件包
  9. Android(安卓)studio的下载和安装
  10. Unity发送参数给Android打开TestActivity