安卓开发日志捕获,错误日志捕获catch,崩溃日志捕获,抓取崩溃日志

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Environment;
import android.util.Log;

import com.foxconn.cesbg.utouch.mvp.model.entity.EventLogBean;
import com.jess.arms.utils.DataHelper;


import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;

public class CrashHandler implements Thread.UncaughtExceptionHandler {

    private static CrashHandler crashUtils;
    private Context mContext;
    private Thread.UncaughtExceptionHandler mDefaultHandler;
    private static final String TAG = "CrashHandler";

    private CrashHandler() {
    }

    public static CrashHandler getInstance() {
        if (crashUtils == null) {
            crashUtils = new CrashHandler();
        }
        return crashUtils;
    }

    /**
     * 初始化
     *
     * @param context
     */
    public void init(Context context) {
        mContext = context;
        //获取系统默认的UncaughtException
        mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
        //将自己的Crash放进去
        Thread.setDefaultUncaughtExceptionHandler(this);
    }

    @Override
    public void uncaughtException(Thread t, Throwable ex) {
        Log.e(TAG, "捕捉到了异常");
        // 1. 获取信息
        // 1.1 崩溃信息
        // 1.2 手机信息
        // 1.3 版本信息
        // 2.写入文件
        String crashFileName = saveInfoToSD(ex);
        Log.e(TAG, "fileName --> " + crashFileName);

        // 3. 缓存崩溃日志文件
        cacheCrashFile(crashFileName);
        // 系统默认处理
        mDefaultHandler.uncaughtException(t, ex);
    }

    /**
     * 缓存崩溃日志文件
     *
     * @param fileName
     */
    private void cacheCrashFile(String fileName) {
        SharedPreferences sp = mContext.getSharedPreferences("crash", Context.MODE_PRIVATE);
        sp.edit().putString("CRASH_FILE_NAME", fileName).commit();
    }


    /**
     * 获取崩溃文件名称
     *
     * @return
     */
    public File getCrashFile() {
        String crashFileName = mContext.getSharedPreferences("crash",
                Context.MODE_PRIVATE).getString("CRASH_FILE_NAME", "");
        return new File(crashFileName);
    }

    /**
     * 保存获取的 软件信息,设备信息和出错信息保存在SDcard中
     *
     * @param ex
     * @return
     */
    private String saveInfoToSD(Throwable ex) {
        String fileName = null;
        String s = "["+obtainSimpleInfo(mContext,ex)+"]";

        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            try {
                //这里我并不需要存很多错误日志,只能存一个
                String path = mContext.getExternalFilesDir("log").getPath();
                File dir = new File(path);
                if (!dir.exists()) {
                    dir.mkdirs();
                }
//                else {
//                    File[] files = dir.listFiles();
//                    for (File file :
//                            files) {
//                        file.delete();
//                    }
//                }
                fileName = path + "/error" + getAssignTime("yyyyMMdd-HH:mm") + ".log";
                FileOutputStream fos = new FileOutputStream(fileName, true);
                fos.write(s.getBytes());
                fos.flush();
                fos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return fileName;
    }

    /**
     * 返回当前日期根据格式
     **/
    private String getAssignTime(String dateFormatStr) {
        DateFormat dataFormat = new SimpleDateFormat(dateFormatStr);
        long currentTime = System.currentTimeMillis();
        return dataFormat.format(currentTime);
    }


    /**
     * 获取一些简单的信息,软件版本,手机版本,型号等信息存放在HashMap中
     *
     * @return
     */
    private String obtainSimpleInfo(Context context,Throwable ex) {
        HashMap<String, String> map = new HashMap<>();
        PackageManager mPackageManager = context.getPackageManager();
        PackageInfo mPackageInfo = null;
        try {
            mPackageInfo = mPackageManager.getPackageInfo(
                    context.getPackageName(), PackageManager.GET_ACTIVITIES);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        EventLogBean eventLogBean = new EventLogBean();
        eventLogBean.setMODEL("" + Build.MODEL);
        eventLogBean.setSDK_INT("" + Build.VERSION.SDK_INT);
        eventLogBean.setPRODUCT("" + Build.PRODUCT);
        eventLogBean.setAgent("versionName:"+mPackageInfo.versionName+"----versionCode"+mPackageInfo.versionCode+"----"+getMobileInfo(context));
        String s = JsonUtil.gsonString(eventLogBean);//bean实体转string字符串,此方法可以自行百度,在这就不提供了
        return s;
    }


    /**
     * Cell phone information
     *
     * @return
     */
    public static String getMobileInfo(Context context) {
        StringBuffer sb = new StringBuffer();
        sb.append("mac:").append(SystemUtils.getMacAddressMAC(context)).append("----")
                .append("DeviceBrand:").append(SystemUtils.getDeviceBrand()).append("----")
                .append("DeviceModel:").append(SystemUtils.getDeviceModel()).append("----")
                .append("SystemVersion:").append(SystemUtils.getSystemVersion()).append("----")
                .append("DeviceProductName:").append(SystemUtils.getDeviceProductName(context));
        return sb.toString();
    }


    /**
     * 获取系统未捕捉的错误信息
     *
     * @param throwable
     * @return
     */
    private String obtainExceptionInfo(Throwable throwable) {
        StringWriter stringWriter = new StringWriter();
        PrintWriter printWriter = new PrintWriter(stringWriter);
        throwable.printStackTrace(printWriter);
        printWriter.close();
        return stringWriter.toString();
    }
}

在application中初始化

CrashHandler.getInstance().init(this);

获取存入本地的错误日志

private String crashSet() {
        File file = getCrashFile();
        FileInputStream fis;

        try {

            fis = new FileInputStream(file);

        } catch (FileNotFoundException e) {

            e.printStackTrace();

            return "";

        }

        BufferedReader br = new BufferedReader(new InputStreamReader(fis));

        String str = null;

        try {

            str = br.readLine();

        } catch (IOException e) {

            e.printStackTrace();

        }
        return str;
    }

    /**
     * 获取崩溃文件名称
     *
     * @return
     */
    public File getCrashFile() {
        String crashFileName = mRootView.getActivity().getSharedPreferences("crash",
                Context.MODE_PRIVATE).getString("CRASH_FILE_NAME", "");
        return new File(crashFileName);
    }

上一篇:ORA-28009:connection as SYS should be as SYSDBA OR SYSOPER解决方法


下一篇:SpringBoot笔记(二):配置文件