您的位置:首页 > 移动开发 > Android开发

Android 全局崩溃日志(CrashHandle)记录

2017-06-19 09:31 295 查看
一、全局日志的初始化

在自定义Application中添加此方法,并在自定义Application的onCreate中调用

private void initCrashhandle() {
CrashHandle crashHandler = CrashHandle.getInstance();
// 注册crashHandler
crashHandler.init(getApplicationContext());
}

二、CrashHandle类

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;

import com.adas.responsibleinvestigationplatform.greendao.daoutils.JDLogManage;

import java.io.File;
import java.io.FilenameFilter;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.Thread.UncaughtExceptionHandler;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Properties;
import java.util.TreeSet;

public class CrashHandle implements UncaughtExceptionHandler {
/** Debug Log Tag */
public static final String TAG = "CrashHandler";
/** 是否开启日志输出, 在Debug状态下开启, 在Release状态下关闭以提升程序性能 */
public static final boolean DEBUG = true;
/** CrashHandler实例 */
private static CrashHandle INSTANCE;
/** 程序的Context对象 */
private Context mContext;
/** 系统默认的UncaughtException处理类 */
private UncaughtExceptionHandler mDefaultHandler;

/** 使用Properties来保存设备的信息和错误堆栈信息 */
private Properties mDeviceCrashInfo = new Properties();
private static final String VERSION_NAME = "versionName";
private static final String VERSION_CODE = "versionCode";
private static final String STACK_TRACE = "STACK_TRACE";
/** 错误报告文件的扩展名 */
private static final String CRASH_REPORTER_EXTENSION = ".cr";

/** 保证只有一个CrashHandler实例 */
private CrashHandle() {
}

/** 获取CrashHandler实例 */
public static synchronized CrashHandle getInstance() {
if (INSTANCE == null) {
INSTANCE = new CrashHandle();
}
return INSTANCE;
}

/**
* 初始化,注册Context对象, 获取系统默认的UncaughtException处理器, 设置该CrashHandler为程序的默认处理器
*
* @param ctx
*/
@SuppressWarnings("static-access")
public void init(Context ctx) {
mContext = ctx;
sp = mContext.getSharedPreferences("config", mContext.MODE_PRIVATE);
mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(this);
}

private SharedPreferences sp;

@Override
public void uncaughtException(Thread thread, Throwable ex) {

if (!handleException(ex) && mDefaultHandler != null) {
// 如果用户没有处理则让系统默认的异常处理器来处理
mDefaultHandler.uncaughtException(thread, ex);
} else {
// Sleep一会后结束程序
// 来让线程停止一会是为了显示Toast信息给用户,然后Kill程序
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
Log.e(TAG, "Error : ", e);
}

}
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
}

/**
* 自定义错误处理,收集错误信息 发送错误报告等操作均在此完成. 开发者可以根据自己的情况来自定义异常处理逻辑
*
* @param ex
* @return true:如果处理了该异常信息;否则返回false
*/
private boolean handleException(Throwable ex) {
if (ex == null) {
return true;
}
final String msg = ex.getLocalizedMessage();
// 使用Toast来显示异常信息
new Thread() {
@Override
public void run() {
// Toast 显示需要出现在一个线程的消息队列中
Looper.prepare();
Toast.makeText(mContext, "程序出错啦:" + msg, Toast.LENGTH_LONG).show();
Looper.loop();
}
}.start();
// 收集设备信息
collectCrashDeviceInfo(mContext);
/**
* 保存错误报告文件
* 在系统崩溃的时候可能会直接弹出app,没法发出错误日志
* 我没在这里发生错误日志【在项目启动页开启service来发送错误日志】
*/
saveCrashInfoToFile(ex);
return true;
}

/**
* 收集程序崩溃的设备信息
*
* @param ctx
*/
public void collectCrashDeviceInfo(Context ctx) {
try {
// Class for retrieving various kinds of information related to the
// application packages that are currently installed on the device.
// You can find this class through getPackageManager().
PackageManager pm = ctx.getPackageManager();
// getPackageInfo(String packageName, int flags)
// Retrieve overall information about an application package that is
// installed on the system.
// public static final int GET_ACTIVITIES
// Since: API Level 1 PackageInfo flag: return information about
// activities in the package in activities.
PackageInfo pi = pm.getPackageInfo(ctx.getPackageName(),
PackageManager.GET_ACTIVITIES);
if (pi != null) {
// public String versionName The version name of this package,
// as specified by the <manifest> tag's versionName attribute.
mDeviceCrashInfo.put(VERSION_NAME,
pi.versionName == null ? "not set" : pi.versionName);
// public int versionCode The version number of this package,
// as specified by the <manifest> tag's versionCode attribute.
mDeviceCrashInfo.put(VERSION_CODE, pi.versionCode);
}
} catch (NameNotFoundException e) {
Log.e(TAG, "Error while collect package info", e);
}
// 使用反射来收集设备信息.在Build类中包含各种设备信息,
// 例如: 系统版本号,设备生产商 等帮助调试程序的有用信息
// 返回 Field 对象的一个数组,这些对象反映此 Class 对象所表示的类或接口所声明的所有字段
Field[] fields = Build.class.getDeclaredFields();
for (Field field : fields) {
try {
// setAccessible(boolean flag)
// 将此对象的 accessible 标志设置为指示的布尔值。
// 通过设置Accessible属性为true,才能对私有变量进行访问,不然会得到一个IllegalAccessException的异常
field.setAccessible(true);
mDeviceCrashInfo.put(field.getName(), field.get(null));
if (DEBUG) {
Log.d(TAG, field.getName() + " : " + field.get(null));
}
} catch (Exception e) {
Log.e(TAG, "Error while collect crash info", e);
}
}
}

/**
* 保存错误信息到文件中
*
* @param ex
* @return
*/
private String saveCrashInfoToFile(Throwable ex) {
Writer info = new StringWriter();
PrintWriter printWriter = new PrintWriter(info);
// printStackTrace(PrintWriter s)
// 将此 throwable 及其追踪输出到指定的 PrintWriter
ex.printStackTrace(printWriter);
// getCause() 返回此 throwable 的 cause;如果 cause 不存在或未知,则返回 null。
Throwable cause = ex.getCause();
while (cause != null) {
cause.printStackTrace(printWriter);
cause = cause.getCause();
}
// toString() 以字符串的形式返回该缓冲区的当前值。
String result = info.toString();
printWriter.close();
mDeviceCrashInfo.put(STACK_TRACE, result);
String msg = mDeviceCrashInfo.toString();

/**
* 以下几行代码是手机错误信息的代码
* 1.JLog是自定义Log类,在这里打印崩溃日志
* 2.JDLogManage是自定义【数据库操作管理类】——用于保存、取出和删除崩溃日志
*/
JLog.d(msg);
//生成错误日志并保存
JDLogManage manage = new JDLogManage(mContext);
manage.insertLog(msg);
return msg;
}

/**
* 把错误报告发送给服务器,包含新产生的和以前没发送的.
* 发送后标记为已发送/或者删除日志
* @param ctx
*/
private void sendCrashReportsToServer(Context ctx) {
String[] crFiles = getCrashReportFiles(ctx);
if (crFiles != null && crFiles.length > 0) {
TreeSet<String> sortedFiles = new TreeSet<String>();
sortedFiles.addAll(Arrays.asList(crFiles));

for (String fileName : sortedFiles) {
//可回传文件
File cr = new File(ctx.getFilesDir(), fileName);
cr.delete();// 删除已发送的报告
}
}
}

/**
* 获取错误报告文件名
*
* @param ctx
* @return
*/
private String[] getCrashReportFiles(Context ctx) {
File filesDir = ctx.getFilesDir();
// 实现FilenameFilter接口的类实例可用于过滤器文件名
FilenameFilter filter = new FilenameFilter() {
// accept(File dir, String name)
// 测试指定文件是否应该包含在某一文件列表中。
public boolean accept(File dir, String name) {
return name.endsWith(CRASH_REPORTER_EXTENSION);
}
};
// list(FilenameFilter filter)
// 返回一个字符串数组,这些字符串指定此抽象路径名表示的目录中满足指定过滤器的文件和目录
return filesDir.list(filter);
}

/**
* 在程序启动时候, 可以调用该函数来发送以前没有发送的报告
*/
public void sendPreviousReportsToServer() {
sendCrashReportsToServer(mContext);
}
}

三、错误日志回传IntentService

public class UploadJDLogService extends IntentService {

private JDLogManage manage;
private List<JDLog> logs;
private Gson gson;

public UploadJDLogService() {
super("UploadJDLogService");
}

@Override
public void onCreate() {
super.onCreate();
gson = JApplication.getGson();
manage = new JDLogManage(this);
String userGuid = SharedPreferencesUtil.getUserGuid(this);
if (userGuid == null || userGuid == "") {
return;
}
logs = manage.getUnUploadLog(userGuid);
}

@Override
protected void onHandleIntent(Intent intent) {
if (logs != null && logs.size() > 0) {
for (int i = 0; i < logs.size(); i++) {
post
c8ac
Logs(logs.get(i));
}
}
stopSelf();
}

//回传日志
private void postLogs(JDLog log) {
final String JSONString = gson.toJson(log);
byte jsonbyte[];
try {
jsonbyte = JSONString.getBytes("UTF-8");//json转byte
} catch (Exception e) {
e.printStackTrace();
return;
}
String url = CommConfig.PATH.SendErrorLog;
try {
Response response = OkHttpUtils.postByte()
.url(url)
.content(jsonbyte)
.build()
.connTimeOut(Constant.Sorket_TimeOut)
.readTimeOut(Constant.Sorket_TimeOut)
.writeTimeOut(Constant.Sorket_TimeOut)
.execute();
if (response.isSuccessful()) {
String responseString = response.body().string();
String result = URLResultPraise.praiseUnCompress(responseString, UploadJDLogService.this);
ResultEntity result1 = new ResultEntity(result, UploadJDLogService.this);
if (result1.isOk()) {
manage.setUploaded(log);
JLog.d("日志回传成功");
}else {

}
}else {

}
} catch (IOException e) {
e.printStackTrace();
}
}
}

4、数据库管理类【JDLog的属性可以通过此类推敲出来,就不展示了】【这里用了数据库框架greendao】

public class JDLogManage {
private JDLogDao dao;
private Context context;
private final int clientType = 3;//1后台网址,3安卓督导Android,4ios督导 5安卓访员 6苹果访员

public JDLogManage(Context context) {
this.context = context;
dao = GreenDaoManager.getInstance().getUpdateSession(context).getJDLogDao();
}

/**
* 插入一个数据,返回插入的主键
*
* @param msg
* @return
*/
public Long insertLog(String msg) {
if (msg == null) return null;
JDLog infoLog = new JDLog();
infoLog.setUserName(SharedPreferencesUtil.getUserName(context));
infoLog.setUserTel(SharedPreferencesUtil.getUserTelephone(context));
infoLog.setUserGuid(SharedPreferencesUtil.getUserGuid(context));
infoLog.setClientTime(Utils.getCurrentTime());
infoLog.setMechinecode(Utils.getDeviceIMEI(context));
infoLog.setUploadStatus(0);
infoLog.setLogMsg(msg);
infoLog.setBrand(Build.BRAND);
infoLog.setSystemVersion(Utils.getSystemVersion());
infoLog.setAppVersion(CommConfig.VERSION);
infoLog.setClientType(clientType);
infoLog.setLogType(1);//常规崩溃日志
infoLog.setDevMode(android.os.Build.MODEL);
return dao.insert(infoLog);
}

//获取未上传数据
public List<JDLog> getUnUploadLog(String userGuid) {
return dao.queryBuilder().where(JDLogDao.Properties.UploadStatus.eq(0)).list();
}

//设置为已上传
public void setUploaded(JDLog log) {
if (log == null || log.getId() == null) return;
log.setUploadStatus(1);
dao.update(log);
}

//删除已上传数据【备份回传后删除】
public void deleteAll() {
dao.deleteAll();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: