欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android 未捕获异常crash崩溃日志的截取

程序员文章站 2024-03-20 17:02:22
...

Android应用不可避免地会发生crash,也成之为崩溃。无论你的程序写得多么的完美,总是无法完全避免crash的发生,可能是由于Android系统底层的bug,也可能是由于不充分的机型适配或者糟糕的网络情况,当crash发生时,系统会kill掉正在执行的程序,现象是闪退或者提示用户程序已经停止运行。这是对用户来说很不友好,也是开发者不愿意看到的。


但是也并不是没有办法捕获,其实系统给我们提供了Tread.UncaughtExceptionHandler就是默认的未捕获异常的处理,我们只要定义一个类实现它,然后重写uncaughtException方法,把异常打印出来就可以了。


STEP 1:

/**
 * function: 截获崩溃日志:当程序产生未捕获异常
 * Created by lzq on 2017/8/10.
 */

public class CrashHandler implements Thread.UncaughtExceptionHandler{

    //手机存储路径
    private final String logPath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "Crash";

    private static final String TAG = CrashHandler.class.getSimpleName();

    /** 系统默认的异常处理器(默认情况下,系统会终止当前的异常程序) */
    private Thread.UncaughtExceptionHandler mDefaultCrashHandler;
    private Context mContext;
    private String mProcessName;

    /** CrashHandler 实例 */
    private static CrashHandler instance = new CrashHandler();

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

    /** 单例模式 */
    public static CrashHandler getInstance(){
       return instance;
    }


    /** 初始化 */
    public void init(Context context){
        mDefaultCrashHandler = Thread.getDefaultUncaughtExceptionHandler();

        //将当前实例设置为系统默认的异常处理器
        Thread.setDefaultUncaughtExceptionHandler(this);

        mContext = context.getApplicationContext();
        mProcessName = context.getPackageName();
    }

    /** 用于格式化日期,作为日志文件名的一部分. */
    private final DateFormat formatter = new SimpleDateFormat("MMdd-HH:mm:ss", Locale.getDefault());


    @Override
    public void uncaughtException(Thread thread, Throwable throwable) {
        Log.e(TAG, "---------------uncaughtException start---------------\r\n");
        try {
            dumpExceptionToLocalFile(thread, throwable); //把异常日志在手机本地文件输出
            uploadExceptionToServer();  //上传crash日志到服务器
        } catch (IOException e) {
            Log.e(TAG, "uncaughtException,ex:" + e.getMessage());
            e.printStackTrace();
        }

        if (mDefaultCrashHandler != null){
            mDefaultCrashHandler.uncaughtException(thread, throwable);
        }else{
            Process.killProcess(Process.myPid());
        }

        Log.e(TAG, "---------------uncaughtException end---------------\r\n");
    }

    /** dump UncaughtException into local file */
    private void dumpExceptionToLocalFile(Thread thread, Throwable throwable) throws IOException{
        //记录数量达到10个就清理数据
        File logDir = new File(logPath);
        if (logDir.exists()){
            clearExLogWhenMax(logDir);
        }else{
            logDir.mkdirs();
        }

        //错误信息文件名
        Date date = new Date();
        String logFileName = formatter.format(date) + String.format("[%s]", thread.getName())+ ".txt";
        File logExFile = new File(logDir, logFileName);
        logExFile.createNewFile();

        //写入信息到文件中
        try {
            PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(logExFile)));

            //时间戳
            pw.println("Time stamp:" + date);

            pw.println("Process[" + mProcessName + "," + Process.myPid() + "]");

            pw.println();

            //手机信息
            dumpPhoneInfo(pw);

            pw.println();

            //导出异常调用栈信息
            throwable.printStackTrace(pw);
            pw.close();

        }catch (IOException ex){
            Log.e(TAG, "dump info failed");
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

    }


    /**
     * 设置最大日志数量 10
     * @param logDir 日志目录
     */
    private void clearExLogWhenMax(File logDir){

        File[] logFileList = logDir.listFiles();
        if (logFileList == null || logFileList.length == 0){
            return;
        }

        int length = logFileList.length;
        if (length > 10){
            for (File aFile : logFileList){
                try {
                    if (aFile.delete()){
                        Log.d(TAG, "clearExLogWhenMax:" + aFile.getName());
                    }
                }catch (Exception ex){
                    Log.d(TAG, "clearExLogWhenMax:" + ex);
                }
            }
        }

    }


    private void dumpPhoneInfo(PrintWriter pw) throws IOException, PackageManager.NameNotFoundException {
        PackageManager pm = mContext.getPackageManager();
        PackageInfo pi = pm.getPackageInfo(mContext.getPackageName(), PackageManager.GET_ACTIVITIES);
        pw.println("App Version:" + pi.packageName + "_" + pi.versionCode);
        //Android版本号
        pw.println("OS Version:" + Build.VERSION.RELEASE + "_" + Build.VERSION.SDK_INT);

        //手机制造商
        pw.println("Vendor:" + Build.MANUFACTURER);

        //手机型号
        pw.println("Model:" + Build.MODEL);

        //CPU架构
        pw.println("CPU ABI:" + Build.CPU_ABI);

    }


    private void uploadExceptionToServer() {
        //TODO Upload Exception Message To Your Web Server
    }

}

STEP 2:

在整个应用入口文件中进行初始化

public class MyApp extends Application{

    @Override
    public void onCreate() {
        super.onCreate();
        CrashHandler.getInstance().init(this);
    }
}


STEP 3:

点击Button按钮,抛出一个自己定义的异常,就会产生Crash,而后在手机外存中找到捕获异常的文件。

public class MainActivity extends AppCompatActivity {

    private Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn = (Button) findViewById(R.id.btn);

        checkInitPermissions();

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                throw new RuntimeException("这是自己定义的异常");
            }
        });

    }

    public static final int REQUEST_PERMISSION_CODE = 100;
    private void checkInitPermissions(){

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            String[] permissions = new String[]{
                    //放入需要授予的权限,例如需要写入的权限
                    Manifest.permission.WRITE_EXTERNAL_STORAGE
            };
            if (ContextCompat.checkSelfPermission(this, permissions[0]) != PackageManager.PERMISSION_GRANTED) {
                if (ActivityCompat.shouldShowRequestPermissionRationale(this, permissions[0])){
                    ActivityCompat.requestPermissions(this, permissions, REQUEST_PERMISSION_CODE);
                }
            }
        } else {
            handleAfterPermissions();
        }
    }

    private void  handleAfterPermissions(){

    }


    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_PERMISSION_CODE){
            handleAfterPermissions();
        }
    }
}


crash日志文件存储位置可以自定义,根据这个例子CrashHandler文件中的存储位置,在手机的内部存储Crash/目录下。

Android 未捕获异常crash崩溃日志的截取 Android 未捕获异常crash崩溃日志的截取