生成异常日志
程序员文章站
2022-05-26 23:46:20
...
1.添加com.xzplay.crash包和里面的2个文件到src:
FrameCrashApplication.java
FrameCrashHandle.java
2.修改 AndroidManifest.xml文件:
添加SD卡读写权限
在application节点添加android:name=”com.xzplay.crash.FrameCrashApplication”
3.源码:
FrameCrashApplication.java:
package com.xzplay.crash;
import android.app.Application;
public class FrameCrashApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
FrameCrashHandle crash = FrameCrashHandle.gethInstance();
crash.Initiliaze(getApplicationContext());
}
}
FrameCrashHandle.java:
package com.xzplay.crash;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.Thread.UncaughtExceptionHandler;
import java.text.SimpleDateFormat;
import java.util.Locale;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.os.Looper;
import android.widget.Toast;
public class FrameCrashHandle implements UncaughtExceptionHandler {
private FrameCrashHandle() {
}
private Context mContext;
private static FrameCrashHandle hInstance;
private UncaughtExceptionHandler mDefaultExceptionHandle;
/**
* 初始化实例
*
* @param ctx
*/
public void Initiliaze(Context ctx) {
mContext = ctx;
mDefaultExceptionHandle = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(this);
}
/**
* 获取实例对象
*
* @return
*/
public static FrameCrashHandle gethInstance() {
if (hInstance == null) {
hInstance = new FrameCrashHandle();
}
return hInstance;
}
@Override
public void uncaughtException(Thread thread, Throwable ex) {
if (!CrashHandle(ex) && mDefaultExceptionHandle != null) {
mDefaultExceptionHandle.uncaughtException(thread, ex);
} else {
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
}
}
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(110);
}
public static String getPackVersionInfo(Context activity) {
try {
PackageManager manager = activity.getPackageManager();
PackageInfo info = manager.getPackageInfo(
activity.getPackageName(), 0);
return info.versionName;
} catch (Exception e) {
e.printStackTrace();
return "1.0";
}
}
/**
* 自定义处理句柄
*
* @param ex
* @return
*/
private boolean CrashHandle(Throwable ex) {
if (ex == null)
return false;
final String fileName = String.format("CityStar_Crash_%s.txt",
System.currentTimeMillis());
final StackTraceElement[] stack = ex.getStackTrace();
final String msg = ex.getMessage();
new Thread() {
@Override
public void run() {
Looper.prepare();
File f = new File(Environment.getExternalStorageDirectory(),
fileName);
String s = String.format("很抱歉,程序已经崩溃,请将以下文件提交给技术人员\r\n%s",
f.getAbsolutePath());
Toast.makeText(mContext, s, Toast.LENGTH_LONG).show();
Toast.makeText(mContext, s, Toast.LENGTH_LONG).show();
Toast.makeText(mContext, s, Toast.LENGTH_LONG).show();
try {
FileOutputStream os = new FileOutputStream(f);
SimpleDateFormat df = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss", Locale.getDefault());
os.write(df.format(new java.util.Date()).toString()
.getBytes());
String version = String.format("\r\nVersion:s",
getPackVersionInfo(mContext));
os.write(version.getBytes());
String line = String.format("\r\nCrash-> ");
os.write(line.getBytes());
os.write(msg.getBytes());
os.write(line.getBytes());
for (int i = 0; i < stack.length; i++) {
os.write(stack[i].toString().getBytes());
}
os.flush();
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Looper.loop();
}
}.start();
return false;
}
}
这样一旦程序崩溃,将会在sd卡根目录生成 crash-yyyy-MM-dddd.txt文件