全局捕获异常

这是全局捕获异常,捕获之后存取在本地SD卡中

package com.example.week2text;

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

import com.umeng.commonsdk.UMConfigure;
import com.umeng.socialize.PlatformConfig;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

public class UnCatchHandler  implements Thread.UncaughtExceptionHandler {
    private static UnCatchHandler mUnCatchHandler = new UnCatchHandler();
    private Context mContext;

    public static UnCatchHandler getInstance() {
        return mUnCatchHandler;
    }

    public void init(Context context) {
        //获取默认的系统异常捕获器
        //把当前的crash捕获器设置成默认的crash捕获器
        Thread.setDefaultUncaughtExceptionHandler(this);
        mContext = context.getApplicationContext();
    }

    @Override
    public void uncaughtException(Thread t, Throwable e) {
        try {
            saveSD(e);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    //存储sd卡
    private void saveSD(Throwable throwable) throws Exception {
        if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            Log.i("dj", "return");
            return;
        }

        //获取手机的一些信息
        PackageManager pm = mContext.getPackageManager();
        PackageInfo inFo = pm.getPackageInfo(mContext.getPackageName(), PackageManager.GET_ACTIVITIES);
		
        //获取版本信息
        String versionName = inFo.versionName;
        int versionCode = inFo.versionCode;

        int version_code = Build.VERSION.SDK_INT;

        //Android版本号
        String release = Build.VERSION.RELEASE;
        //手机型号
        String mobile = Build.MODEL;

        //手机制造商
        String mobileName = Build.MANUFACTURER;

        //存储
        String path = Environment.getExternalStorageDirectory().getAbsolutePath();
        Log.i("dj",path);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy_MM_DD_HH_mm_ss");
        String time = simpleDateFormat.format(new Date());

        File f = new File(path, "exception");
        f.mkdirs();

        File file = new File(f.getAbsolutePath(), "exception" + time + ".txt");

        if (!file.exists()) {
            file.createNewFile();
        }

        String data = "\nMobile型号:" + mobile + "\nMobileName:" + mobileName + "\nSDK版本:" + version_code +
                "\n版本名称:" + versionName + "\n版本号:" + versionCode + "\n异常信息:" + throwable.getMessage();

        Log.i("dj", data);

        byte[] buffer = data.trim().getBytes();
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        // 开始写入数据到这个文件。
        fileOutputStream.write(buffer, 0, buffer.length);
        fileOutputStream.flush();
        fileOutputStream.close();
    }
}

然后只需要在全局配置一下就可以了
这句代码写在MyApplication里面,清单文件中在配置一下MyApplication即可

UnCatchHandler.getInstance().init(getApplicationContext());
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值