捕获异常

首先加读写权限

public class MainActivity extends AppCompatActivity {
    private TextView mText;
    private String jsonStr = "{\n" +
            "      \"news_id\": \"13810\",\n" +
            "      \"news_title\": \"\\u7b2c14\\u5c4a\\u6e29\\u5dde\\u56fd\\u9645\\u6c7d\\u8f66\\u5c554\\u6708\\u4e3e\\u884c \\u8bbe9\\u5927\\u4e3b\\u9898\\u5c55\\u9986\",\n" +
            "      \"news_summary\": \"\\u6765\\u81ea\\u524d\\u4e0d\\u4e45\\u4e3e\\u884c\\u7684\\u6e29\\u5dde\\u56fd\\u9645\\u6c7d\\u8f66\\u5c55\\u89c8\\u4f1a\\u7b2c\\u4e00\\u6b21\\u65b0\\u95fb\\u53d1\\u5e03\\u4f1a\\u7684\\u6d88\\u606f\\uff0c 2016\\u7b2c14\\u5c4a\\u6e29\\u5dde\\u56fd\\u9645\\u6c7d\\u8f66\\u5c55\\u89c8\\u4f1a\\u5b9a\\u4e8e4\\u67087-10\\u65e5\\u5728\\u6e29\\u5dde\\u56fd\",\n" +
            "      \"pic_url\": \"http:\\/\\/f.expoon.com\\/sub\\/news\\/2016\\/01\\/21\\/580828_230x162_0.jpg\"\n" +
            "    }";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        try {
//            JSONObject object = new JSONObject(jsonStr);
//            String news_id = object.optString("news_id");
//            Log.e("news_Id",news_id);
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
        mText = findViewById(R.id.Get_Text);
        mText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int i = 0;
                int j = 10;
                int num = j / i;

            }
        });
    }
}
 

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        UnCatchHandler.getInstance().init(getApplicationContext());
    }
}

 

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();
    }
}
 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值