捕获程序crash信息

Posted lotz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了捕获程序crash信息相关的知识,希望对你有一定的参考价值。

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
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.text.SimpleDateFormat;
import java.util.Date;

/**
 * 在Application初始化时进行设置CrashHandler,并调用init()方法,便可以捕获异常
 */
public class CrashHandler implements Thread.UncaughtExceptionHandler 

    private static final String TAG = "CrashHandler";
    private static CrashHandler instance = new CrashHandler();
    private Context mContext;
    private Thread.UncaughtExceptionHandler mDefaultHandler;

    private static final String PATH = Environment.getExternalStorageDirectory().getPath() + "/crash/log/";
    private static final String FILE_NAME = "crash";
    private static final String FILE_SUFFIX = ".trace";

    private CrashHandler()

    

    public static CrashHandler getInstance()
        return instance;
    

    public void init(Context context)
        this.mContext = context.getApplicationContext();
        mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
        Thread.setDefaultUncaughtExceptionHandler(this);
    

    /**
     * 当程序出现未捕获的异常,则调用该方法
     * @param t
     *          出现异常的线程
     * @param e
     *          未捕获异常
     */
    @Override
    public void uncaughtException(Thread t, Throwable e) 
        try
            // 导出异常信息到SD卡中
            dumpExceeptionToSDCard(e);

            // 将异常信息上传到服务器
            upLoadExceptionToServer();
        catch(IOException ioEx)
            ioEx.printStackTrace();
        

        e.printStackTrace();
        // 如果系统提供了默认处理器,由系统处理,否则结束程序
        if(mDefaultHandler != null)
            mDefaultHandler.uncaughtException(t, e);
        else
            Process.killProcess(Process.myPid());
        
    

    private void dumpExceeptionToSDCard(Throwable e) throws IOException
        // 如果SD卡不存在,写入失败
        if(! Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
            Log.w(TAG, "sdCard not exists, dump exception failed");
            return;
        

        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 + FILE_SUFFIX);

        try
            PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file)));
            pw.println(time);
            dumpPhoneInfo(pw);
            pw.println();
            e.printStackTrace(pw);
            pw.close();
        catch (Exception exception)
            Log.w(TAG, "dump crash info failed");
        
    

    /**
     *  导入手机信息
     * @param pw
     */
    private void dumpPhoneInfo(PrintWriter pw) throws PackageManager.NameNotFoundException 
        PackageManager pm = mContext.getPackageManager();
        PackageInfo pi =pm.getPackageInfo(mContext.getPackageName(), PackageManager.GET_ACTIVITIES);

        // app信息
        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()
        // 未实现
    

 

以上是关于捕获程序crash信息的主要内容,如果未能解决你的问题,请参考以下文章

Android怎样捕获应用的crash信息

为啥使用Try,Catch捕获异常,程序依然Crash

获取Android崩溃crash信息并写入日志发送邮件

iOS开发——捕获崩溃信息

为啥使用Try,Catch捕获异常,程序依然Crash

程序异常捕获库 - CrashRpt