android crash存储位置,在Android手机上保存Crash Log
定义CrashLog文件:如果有存储器,则将文件保存在存储器下。否则保存在data目录下。存储器不一定是SD卡,也可能是mount出来的一块存储空间(不是内存)。比如,小米3就没有SD卡。public class CrashLog {
private static Context mContext;
public static final String DIR_FOR_APP = "myApp";
public static final String FILE_NAME_FOR_CRASH__LOG = "crash.txt";
public static void setContext( Context aContext ) {
mContext = aContext.getApplicationContext();
}
public static void writeLog(String log) throws Exception {
File dir = createFileDir(DIR_FOR_APP);
File file = new File(dir.getPath(), FILE_NAME_FOR_CRASH__LOG);
if(!file.exists()){
file.createNewFile();
}
StringBuffer orgStr = new StringBuffer();
FileReader fr = new FileReader(file);
int ch = 0;
while((ch = fr.read()) != -1) {
orgStr.append((char)ch);
}
fr.close();
Calendar c = Calendar.getInstance();
String time = String.format("%2d:", c.get(Calendar.HOUR))+
String.format("%2d:", c.get(Calendar.MINUTE))+
String.format("%2d", c.get(Calendar.SECOND))+" ";
String str = orgStr.toString() + "\r\n" + time + log + "\r\n";
FileWriter fw = new FileWriter(file);
fw.write(str);
fw.close();
}
public static void clearLog(){
String dirPath = getDirPath(DIR_FOR_APP);
if( dirPath != null ) {
File file = new File(FILE_NAME_FOR_CRASH__LOG);
if(file.exists()){
file.delete();
}
}
}
public static String getDirPath(String dirName){
String filePath = null;
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
// 如果有存储器,则返回存储器的路径
filePath = Environment.getExternalStorageDirectory()
+ File.separator + dirName;
} else {// 否则返回data目录的路径
filePath = mContext.getCacheDir().getPath() + File.separator
+ dirName;
}
return filePath;
}
public static File createFileDir(String dirName) {
String filePath = getDirPath(dirName);
if( filePath == null ) {
return null;
}
File destDir = new File(filePath);
if (!destDir.exists()) {
destDir.mkdirs();
}
return destDir;
}
}
2. 定义CrashHandler异常捕获类:当程序抛出异常后,由CrashHandler先捕获。将异常堆栈信息和手机系统信息打印到手机制定位置的log文件后,退出。
public class CrashHandler implements UncaughtExceptionHandler {
private Context mContext;
private static CrashHandler mInstance = null;
// 系统默认的异常处理(默认情况下,系统会终止当前的异常程序)
private UncaughtExceptionHandler mDefaultCrashHandler;
private CrashHandler(Context c) {
// 获取系统默认的异常处理器
mDefaultCrashHandler = Thread
.getDefaultUncaughtExceptionHandler();
// 将当前实例设为系统默认的异常处理器
Thread.setDefaultUncaughtExceptionHandler(this);
// 获取Context,方便内部使用
mContext = c.getApplicationContext();
}
public synchronized static CrashHandler create(Context cxt) {
if (mInstance == null) {
mInstance = new CrashHandler(cxt);
}
return mInstance;
}
/**
* 当UncaughtException发生时会回调该函数来处理
*/
@Override
public void uncaughtException(Thread thread, Throwable ex) {
// TODO Auto-generated method stub
StringBuffer logInfo = new StringBuffer();
logInfo.append("Exception: ");
logInfo.append(ex.getMessage());
logInfo.append("\r\n");
logInfo.append(getStackMsg(ex));
logInfo.append("Phone informations:");
logInfo.append("\r\n");
try {
logInfo.append(getPhoneInfo());
// 导出异常信息到存储器中
CrashLog.writeLog(logInfo.toString());
} catch (Exception e) {
//忽略异常
} finally {
// 如果系统提供了默认的异常处理器,则交给系统去结束程序,
if (mDefaultCrashHandler != null) {
mDefaultCrashHandler.uncaughtException(thread, ex);
} else {
MyApplication.getInstance().exit();
}
}
}
private static String getStackMsg(Throwable e) {
StringBuffer sb = new StringBuffer();
StackTraceElement[] stackArray = e.getStackTrace();
for (int i = 0; i
StackTraceElement element = stackArray[i];
sb.append(element.toString() + "\r\n");
}
return sb.toString();
}
private String getPhoneInfo()
throws NameNotFoundException {
StringBuffer info = new StringBuffer();
// 应用的版本号
PackageManager pm = mContext.getPackageManager();
PackageInfo pi = pm.getPackageInfo(mContext.getPackageName(),
PackageManager.GET_ACTIVITIES);
info.append("App Version Name: ");
info.append(pi.versionName);
info.append("\r\n");
// android版本号
info.append("OS Version: ");
info.append(Build.VERSION.RELEASE);
info.append("_");
info.append(Build.VERSION.SDK_INT);
info.append("\r\n");
// 手机制造商
info.append("Vendor: ");
info.append(Build.MANUFACTURER);
info.append("\r\n");
// 手机型号
info.append("Model: ");
info.append(Build.MODEL);
info.append("\r\n");
// cpu架构
info.append("CPU ABI: ");
info.append(Build.CPU_ABI);
info.append("\r\n");
return info.toString();
}
}
3. 自定义Application类,初始化CrashLog和CrashHandler,并提供完全退出应用的接口exit()。
public class MyApplication extends Application{
// 单例模式
private static MyApplication mAppInstance;
// Activity容器
private List mActivityList = new LinkedList();
public MyApplication(){
}
public static MyApplication getInstance() {
if( mAppInstance == null ) {
mAppInstance = new MyApplication();
}
return mAppInstance;
}
// 每新建一个Activity,都要添加到容器中
public void addActivity(Activity a){
mActivityList.add(a);
}
// 遍历容器中所有Activity并finish
public void exit(){
for (Activity activity : mActivityList) {
activity.finish();
}
System.exit(0);
}
@Override
public void onCreate() {
super.onCreate();
CrashLog.setContext(this);
CrashHandler.create(this);
}
}
android crash存储位置,在Android手机上保存Crash Log相关推荐
- android 三星存储位置,三星Android系统文件夹全解
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 三星android系统的开放,使其用户可以自己查看系统和SD卡中的文件夹.就系统和SD卡中常见的目录代表什么意思,下面是一个较实用的总结: 一.SD卡中 ...
- Android 本地tomcat服务器接收处理手机上传的数据之案例演示
上一篇:Android 本地tomcat服务器接收处理手机上传的数据之环境搭建 本篇基于上一篇搭建的服务器端环境,具体介绍Android真机上传数据到tomcat服务器的交互过程 场景:A ...
- 怎么将手机上保存的CAD图纸发送给好友
现在有很多的工作我们也可以在手机中完成,就比如说我们再收集中将图纸进行查看之后保存了下来,之后同事需要使用到这张CAD图纸文件.那么我们就可以将图纸发送给好友.怎么将手机上保存的CAD图纸发送给好友, ...
- android 短信位置,浅析Android手机卫士之手机实现短信指令获取位置
推荐阅读: 获取位置 新建一个service的包 新建一个GPSService类继承系统的Service类 清单文件中注册一下 重写onCreate()方法,服务创建的时候回调 重写onDestroy ...
- android系统文件重命名文件格式,安卓手机上文件的后缀名怎么改?
安卓手机上文件的后缀名怎么改?以下文字资料是由(历史新知网www.lishixinzhi.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 安卓手机上文件的后缀名怎么改? 用手机修改 ...
- 百度 android 下载文件夹在哪里,手机上从百度网盘下载下来的东西在哪个文件夹里?...
手机上从百度网盘下载下来的东西在哪个文件夹里?以下文字资料是由(历史新知网www.lishixinzhi.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 手机上从百度网盘下载下来的 ...
- android记事本添加图片功能,安卓手机上有什么便签app既可以写日记又可以添加照片?...
原标题:安卓手机上有什么便签app既可以写日记又可以添加照片? 当前,有很多人一直保持着写日记的习惯,因为这样可以及时记录自己的成长轨迹,使得自己可以追寻到时光的记忆,但是我们记录日记的工具,却随着时 ...
- android默认安装位置,修改Android手机的软件默认安装位置
如无意外,一般来说,android 手机的默认安装位置应该是手机内存,因为考虑到如输入法的软件需要常驻使用. 但对于手机 Room 较小的手机,自然是把如游戏等非关键程序安装在 SD 卡好,留出更多的 ...
- android模糊后面视频,在安卓手机上怎么制作中间是横视频上下是模糊效果的竖视频?手机视频短片制作...
注意此教程方案是『安卓手机端教程方案』 今天要介绍的是安卓手机视频短片制作软件,可以在安卓手机上制作手机视频短片哦,制作那种中间是横视频上下是模糊效果的竖视频哦~不是手机看的视频片,手机怎么下载好看的 ...
最新文章
- JZOJ 3870. 【NOIP2014八校联考第4场第1试10.19】单词检索(search)
- Git上传项目到github
- chisel(安装)
- python算法的缺陷和不足_决策树基本概念及算法优缺点
- 前端面试题(一)(CSS篇)建议收藏,持续更新中...
- 基于web的库存管理系统
- Java 打印Hello World
- Microsoft Internet Explorer浏览器包含最新版本的内置Adobe Flash Player的解决办法
- Mac本换ssd注意事项
- word论文页码从任意页开始编号
- Excel合并表格之vba
- JDON 论坛上的NETTY贴
- 哪里有日语50音图的发音啊?
- golang map的遍历
- Linux学习(一)-- df/du命令
- table表格学习归纳
- Python实现K-Means聚类算法
- 慧眼舆情热词分析架构简述
- unity学习 — 关于Unity发布PC端程序,窗口最大化并带有任务栏的实现
- 20190901三毛传