编程语言
首页 > 编程语言> > Android Crash之Java Crash分析,androidflash插件

Android Crash之Java Crash分析,androidflash插件

作者:互联网

解决Java Crash

这里写图片描述

这个时候程序就正常运行了,是不是很简单啊,但这种情况是自己在开发中调试运行时可以通过logcat来定位问题,但如果产品上线之后你怎么办,用户都是小白哦,可不会给你提供错误日志,这个就是本篇文章要讲的重点,如果要让我们自己记录错误日志,怎么做?

通过UncaughtExceptionHandler来记录dump异常日志

package com.devilwwj.androidcrashdemo;

/**

*/

import android.content.Context;

import android.content.pm.PackageInfo;

import android.content.pm.PackageManager;

import android.content.pm.PackageManager.NameNotFoundException;

import android.os.Build;

import android.os.Environment;

import android.os.Process;

import android.util.Log;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileWriter;

import java.io.IOException;

import java.io.PrintWriter;

import java.lang.Thread.UncaughtExceptionHandler;

import java.text.SimpleDateFormat;

import java.util.Date;

public class CrashHandler implements UncaughtExceptionHandler {

private static final String TAG = “CrashHandler”;

private static final boolean DEBUG = true;

private static final String PATH = Environment

.getExternalStorageDirectory() + “/CrashDemo/log/”;

private static final String FILE_NAME = “crash”;

private static final String FILE_NAME_SUFFIX = “.trace”;

private static final String ABOLUTE_PATH = PATH + FILE_NAME + FILE_NAME_SUFFIX;

private String deviceToken;

private static CrashHandler sInstance = new CrashHandler();

private UncaughtExceptionHandler mDefaultCrashHandler;

private Context mContext;

private CrashHandler() {

}

public static CrashHandler getInstance() {

return sInstance;

}

public void init(Context context) {

mDefaultCrashHandler = Thread.getDefaultUncaughtExceptionHandler();

Thread.setDefaultUncaughtExceptionHandler(this);

mContext = context.getApplicationContext();

}

/**

*/

@Override

public void uncaughtException(Thread thread, Throwable ex) {

try {

// 导出异常信息到SD卡中

dumpExceptionToSDCard(ex);

} catch (IOException e) {

e.printStackTrace();

}

ex.printStackTrace();

// 如果系统提供了默认的异常处理器,则交给系统去结束我们的程序,否则就由我们自己结束自己

if (mDefaultCrashHandler != null) {

mDefaultCrashHandler.uncaughtException(thread, ex);

} else {

Process.killProcess(Process.myPid());

}

}

private File dumpExceptionToSDCard(Throwable ex) throws IOException {

// 如果SD卡不存在或无法使用,则无法把异常信息写入SD卡

if (!Environment.getExternalStorageState().equals(

Environment.MEDIA_MOUNTED)) {

if (DEBUG) {

Log.w(TAG, “sdcard unmounted,skip dump exception”);

return null;

}

}

File dir = new File(PATH);

if (!dir.exists()) {

dir.mkdirs();

}

long current = System.currentTimeMillis();

String time = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”)

.format(new Date(current));

// File file = new File(PATH + FILE_NAME + time + “_”+ deviceToken +

// FILE_NAME_SUFFIX);

File file = new File(PATH + FILE_NAME + FILE_NAME_SUFFIX);

if (!file.exists()) {

file.createNewFile();

} else {

try {

// 追加内容

PrintWriter pw = new PrintWriter(new BufferedWriter(

new FileWriter(file, true)));

pw.println(time);

dumpPhoneInfo(pw);

pw.println();

ex.printStackTrace(pw);

pw.println("---------------------------------分割线----------------------------------");

pw.println();

pw.close();

} catch (Exception e) {

Log.e(TAG, “dump crash info failed”);

}

}

return file;

}

private void dumpPhoneInfo(PrintWriter pw) throws NameNotFoundException {

PackageManager pm = mContext.getPackageManager();

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

PackageManager.GET_

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

ACTIVITIES);

pw.print("App Version: ");

pw.print(pi.versionName);

pw.print(’_’);

pw.println(pi.versionCode);

// android版本号

pw.print("OS Version: ");

pw.print(Build.VERSION.RELEASE);

pw.print("_");

pw.println(Build.VERSION.SDK_INT);

// 手机制造商

pw.print("Vendor: ");

pw.println(Build.MANUFACTURER);

// 手机型号

pw.print("Model: ");

pw.println(Build.MODEL);

// cpu架构

pw.print("CPU ABI: ");

pw.println(Build.CPU_ABI);

}

/**

*/

private void uploadExceptionToServer(File log) {

// TODO Upload Exception Message To Your Web Server

}

标签:插件,Java,pw,private,import,print,Crash,android,new
来源: https://blog.csdn.net/m0_64319298/article/details/121726159