第一、首先写线程捕捉异常的Thread.UncaughtExceptionHandler 类
第二、创建MyAppliciton类,要在清单文件中注册Application类 读写权限
捕捉异常的类
package com.example.exceptionhandling.handler;
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 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 context;
public static UnCatchHandler getInstance(){
return mUnCatchHandler;
}
public void init(Context context) {
//获取默认的系统异常捕获器
//把当前的crash捕获器设置成默认的crash捕获器
Thread.setDefaultUncaughtExceptionHandler(this);
context = context.getApplicationContext();
}
@Override
public void uncaughtException(Thread t, Throwable e) {
try {
saveSD(e);
} catch (Exception e1) {
e1.printStackTrace();
}
}
//存储sd卡
private void saveSD(Throwable throwable) throws Exception {
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
Log.i(“dj”, “return”);
return;
}
//获取手机的一些信息
PackageManager pm = context.getPackageManager();
PackageInfo inFo = pm.getPackageInfo(context.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类中调用Handler类的方法
package com.example.exceptionhandling.applicaiton;
import android.app.Application;
import com.example.exceptionhandling.handler.UnCatchHandler;
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
UnCatchHandler.getInstance().init(getApplicationContext());
}
}
package com.example.exceptionhandling;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
MainActivit中按钮的作用
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button yichang;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
yichang = (Button) findViewById(R.id.yichang);
yichang.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.yichang:
int i=0;
int j=10;
int m=j/i;
break;
}
}
}