android 保存成功提示错误,android 错误日志保存到本地

public class CaptureCrashException implements UncaughtExceptionHandler {

/** 系统默认的UncaughtException处理类 */private UncaughtExceptionHandler mDefaultHandler;

/** CaptureCrashException实例 */private static CaptureCrashExceptioninstance;

/** 程序的Context对象 */private Context mContext;

/** 使用Properties来保存设备的信息和错误堆栈信息 */

private static final String VERSION_NAME="versionName";

private static final String VERSION_CODE="versionCode";

/** 保证只有一个CaptureCrashException实例 */

privateCaptureCrashException() {}

/** 获取CrashHandler实例 ,单例模式 */

public static CaptureCrashException getInstance() {

if(instance==null) {instance=new CaptureCrashException();}returninstance;}

/*** 初始化,注册Context对象, 获取系统默认的UncaughtException处理器,

* 设置该CaptureCrashException为程序的默认处理器*/

public voidinit(Context ctx) {

mContext= ctx;

mDefaultHandler= Thread.getDefaultUncaughtExceptionHandler();

Thread.setDefaultUncaughtExceptionHandler(this);

}

/**

* 当UncaughtException发生时会转入该函数来处理

*/

@Override

public void uncaughtException(Thread thread, Throwable ex) {

if(!handleException(ex) &&mDefaultHandler!=null) {

// 如果用户没有处理则让系统默认的异常处理器来处理

mDefaultHandler.uncaughtException(thread, ex);

}else{

newThread() {

@Override

public voidrun() {

SystemClock.sleep(2000);

MyApplication.getInstance().removeAll();

}

}.start();

}

}

/**

* 自定义错误处理,收集错误信息 发送错误报告等操作均在此完成. 开发者可以根据自己的情况来自定义异常处理逻辑

*

*@paramex

*@returntrue:如果处理了该异常信息;否则返回false

*/

private booleanhandleException(Throwable ex) {

if(ex ==null) {

return true;

}

// 输出到日志

StringWriter sw =newStringWriter();

PrintWriter pw =newPrintWriter(sw);

ex.printStackTrace(pw);

MyLogger.systemlLog().e(sw.toString());

// 保存异常日志到sd卡

saveErrorLog(ex);

// 收集设备信息

saveDeviceInfo();

// 发送异常信息到服务器

// 使用Toast友好提示

newThread() {

@Override

public voidrun() {

Looper.prepare();

ToolAlert.showCustomShortToast("程序崩溃了, 重新打开试试!");

Looper.loop();

}

}.start();

return true;

}

/**

* 保存异常日志

*

*@paramex

*/

public voidsaveErrorLog(Throwable ex) {

String errorlog ="";

String savePath ="";

String logFilePath ="";

FileWriter fw =null;

PrintWriter pw =null;

try{

// 判断是否挂载了SD卡

String storageState = Environment.getExternalStorageState();

if(storageState.equals(Environment.MEDIA_MOUNTED)) {

savePath = ConstantManager.logPath;

File file =newFile(savePath);

if(!file.exists()) {

file.mkdirs();

}

errorlog ="errorlog "

+ ToolDateTime.getdateTime()

+".txt";

logFilePath = savePath + errorlog;

}

// 没有挂载SD卡, 无法写文件

if(logFilePath =="") {

return;

}

File logFile =newFile(logFilePath);

if(!logFile.exists()) {

logFile.createNewFile();

}

fw =newFileWriter(logFile,true);

pw =newPrintWriter(fw);

ex.printStackTrace(pw);

pw.close();

fw.close();

}catch(Exception e) {

e.printStackTrace();

}finally{

if(pw !=null) {

pw.close();

}

if(fw !=null) {

try{

fw.close();

}catch(IOException e) {

}

}

}

}

/**

* 保存设备信息

*

*@param

*/

public voidsaveDeviceInfo() {

String errorlog ="";

String savePath ="";

String logFilePath ="";

FileWriter fw =null;

PrintWriter pw =null;

try{// 判断是否挂载了SD卡

String storageState = Environment.getExternalStorageState();

if(storageState.equals(Environment.MEDIA_MOUNTED)) {

savePath = ConstantManager.logPath;

File file =newFile(savePath);

if(!file.exists()) {

file.mkdirs();

}

errorlog ="deviceInfo "

+ ToolDateTime.getdateTime()

+".txt";

logFilePath = savePath + errorlog;

}

// 没有挂载SD卡, 无法写文件

if(logFilePath =="") {

return;

}

File logFile =newFile(logFilePath);

if(!logFile.exists()) {

logFile.createNewFile();

}

fw =newFileWriter(logFile,true);

pw =newPrintWriter(fw);

// 版本号

PackageManager pm =mContext.getPackageManager();

PackageInfo pi = pm.getPackageInfo(mContext.getPackageName(),

PackageManager.GET_ACTIVITIES);

if(pi !=null) {

pw.println(VERSION_NAME+"="+ pi.versionName==null?"not set"

: pi.versionName);

pw.println(VERSION_CODE+"="+ pi.versionCode);

}

// 使用反射来收集设备信息.在Build类中包含各种设备信息,

// 例如: 系统版本号,设备生产商 等帮助调试程序的有用信息

Field[] fields = Build.class.getDeclaredFields();

for(Field field : fields) {

field.setAccessible(true);

pw.println(field.getName() +"="+ field.get(null));

}

pw.close();

fw.close();

}catch(Exception e) {

e.printStackTrace();

}finally{

if(pw !=null) {

pw.close();

}

if(fw !=null) {

try{

fw.close();

}catch(IOException e) {

}

}

}

}

/**

* 清空错误日志缓存目录

*/

public voidclearSavePathFile() {

File file =newFile(ConstantManager.logPath);

ToolFile.deleteFileOrDir(file);

}

}

在LauncherAcivity oncrease 中初始化

// 开启线程, 进行初始化操作

newThread() {

@Override

public voidrun() {

// 初始化CaptureCrashException

CaptureCrashException.getInstance().init(MyApplication.getMyApplication());

//"初始化完成"

initCacheDirectory();

}

}.start();

private void initCacheDirectory(){

File logPath =newFile(ConstantManager.logPath);

if(!logPath.isDirectory() || !logPath.exists()) {

logPath.mkdir();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值