每当我们做项目 的时候,都会有一些潜在的不易被发现的Bug在某一个小角落瑟瑟发抖,而这些将成为项目上线后的最大隐患,他们伺机而动后直线降低用户体验,那么怎么在第一时间掌握这些BUG并解决它呢?接下来我们就聊聊这个事情
各位客官往下看↓↓↓
1.首先创建一个类SaveExceptionUtil implements UncaughtExceptionHandler接口
代码实例:
public class SaveExceptionUtil implements Thread.UncaughtExceptionHandler {
private static final boolean FLAG =true;
//log文件本地保存的路径
private static final StringPATH = Environment.getExternalStorageDirectory().getPath() +"/saveEP/log/";
private static final StringFILE_NAME ="AppLog";//文件名称
private static final StringFLIE_NAME_SUFFIX =".log";//后缀名
private Thread.UncaughtExceptionHandler mDefaultExceptionHandle;
private ContextmContext;
private Filefile;
public SaveExceptionUtil() {
}
public static SaveExceptionUtil getInstance() {
return InstanceHolder.sInstance;
}
private static class InstanceHolder {
private static SaveExceptionUtil Instance =new SaveExceptionUtil();
}
public void init(Context context) {
mDefaultExceptionHandle = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(this);
mContext = context.getApplicationContext();
}
@Override
public void uncaughtException(Thread thread, Throwable ex) {
try {
saveExceptionToSDCard(ex);
// uploadExceptionToServer(); // 上传到服务器
}catch (IOException e) {
e.printStackTrace();
}
}
private void saveExceptionToSDCard(final Throwable ex)throws IOException {
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
return;
}
File dir =new File(PATH);
if (!dir.exists()) {
Log.d(TAG, "log dir not exists. ready create");
dir.mkdirs(); //创建文件夹
}
long currentTime = System.currentTimeMillis();
final String time =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(currentTime ));
file =new File(PATH +FILE_NAME +FILE_NAME_);
new Thread(new Runnable() {
@Override
public void run() {
try {
PrintWriter pw =new PrintWriter(new BufferedWriter(new FileWriter(file)));
pw.println(time);
dumpPhoneInfo(pw, ex);
pw.close();
}catch (Exception e) {
}
}
}).start();
}
private void obtainPhoneInfo(PrintWriter pw, Throwable ex)throws PackageManager.NameNotFoundException {
PackageManager pm =mContext.getPackageManager();
PackageInfo pi = pm.getPackageInfo(mContext.getPackageName(), PackageManager.GET_ACTIVITIES);
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(" _ sdk: ");
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);
pw.println();
//异常信息
ex.printStackTrace(pw);
}
}
到此,这个类就完成了。 客官可以 int i = 10/0 来测试一发看看你的sd卡中有没有这个Exception的文件保存(别忘了给SD卡的权限喔...)。
2.接下来就聊一聊这个上传到服务器的事情。废话不多直接上代码
客官您请↓↓↓
private void uploadExceptionToServer() {
String PATH = Environment.getExternalStorageDirectory().getPath() +"/saveEP/log/";
String FILE_NAME ="Applog";
String FLIE_NAME_SUFFIX =".log";
String str = PATH + FILE_NAME +FLIE_NAME_POINT;
String hair_timeline ="你的服务器地址";
File file =new File(str);
if (file.exists()) {
ArrayMap map =new ArrayMap<>();
map.put("filelog",file);
//访问网络上传文件 这段可根据自己情况来操作
OkHttpUtils.post()
.url(hair_timeline)
.addParams("login_session", MyApplication.Sessions)
.files("filelog", map)
.build()
.execute(new StringCallback() {
@Override
public void onError(Call call, Exception e, int id) {
}
@Override
public void onResponse(String response, int id) {
try {
JSONObject jsonObject =new JSONObject(response);
String status = jsonObject.getString("status");
String msg = jsonObject.getString("msg");
if ("200".equals(status)) {
MyToast.getToast(getContext(), msg);
}else {
MyToast.getToast(getContext(), msg);
}
}catch (JSONException e) {
e.printStackTrace();
}
}
});
}
}
上传服务器的代码,写在首页加载的initView方法中,在App崩掉后,用户二次打开时上传到服务器。
3. 初始化我们写的这个类。创建一个类 MyApplication 继承 Application,当然在做项目的时候肯定已经创建继承了因为你有许许多多的初始化要在这里做,在MyApplication的onCareat方法中初始化,
//全局bug捕获初始化
SaveExceptionUtil.getInstance().init(this);
到此,今天的事情就聊的差不多了,客官慢走。