一旦在UncaughtExceptionHandler中捕获了错误,该应用程序将冻结一段时间,并在一段时间后得到该应用程序没有响应的响应->“等待”或->“关闭”。
我使用UncaughtExceptionHandler的原因是,一旦发生崩溃,应用程序会将崩溃报告写入文件。崩溃被写入文件。该应用一旦崩溃便被冻结。我需要显示一个自定义活动,以显示“对不起崩溃..”或通常的窗口。不幸的是,该应用程序已关闭。
一旦崩溃已写入文件,我打算将文件分批发布。成功发布文件后,我需要删除此文件。是否可以编写一个函数来执行上述发布并在将崩溃报告写入文件后立即调用该函数?
代码:
public class UncaughtException implements UncaughtExceptionHandler {
private Context context;
private static Context context1;
public UncaughtException(Context ctx) {
context = ctx;
context1 = ctx;
}
private StatFs getStatFs() {
File path = Environment.getDataDirectory();
return new StatFs(path.getPath());
}
@SuppressWarnings("deprecation")
private long getAvailableInternalMemorySize(StatFs stat) {
long blockSize, availableBlocks;
if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
blockSize = stat.getBlockSizeLong();
availableBlocks = stat.getAvailableBlocksLong();
} else {
blockSize = stat.getBlockSize();
availableBlocks = stat.getAvailableBlocks();
}
return availableBlocks * blockSize;
}
@SuppressWarnings("deprecation")
private long getTotalInternalMemorySize(StatFs stat) {
long blockSize, totalBlocks;
if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
blockSize = stat.getBlockSizeLong();
totalBlocks = stat.getBlockCountLong();
} else {
blockSize = stat.getBlockSize();
totalBlocks = stat.getBlockCount();
}
return totalBlocks * blockSize;
}
private void addInformation(StringBuilder message) {
message.append("Locale: ").append(Locale.getDefault()).append('\n');
try {
PackageManager pm = context.getPackageManager();
PackageInfo pi;
pi = pm.getPackageInfo(context.getPackageName(), 0);
message.append("Version: ").append(pi.versionName).append('\n');
message.append("Package: ").append(pi.packageName).append('\n');
} catch (Exception e) {
Log.e("CustomExceptionHandler", "Error", e);
message.append("Could not get Version information for ").append(
context.getPackageName());
}
message.append("Phone Model ").append(android.os.Build.MODEL)
.append('\n');
message.append("Android Version : ")
.append(android.os.Build.VERSION.RELEASE).append('\n');
message.append("Board: ").append(android.os.Build.BOARD).append('\n');
message.append("Brand: ").append(android.os.Build.BRAND).append('\n');
message.append("Device: ").append(android.os.Build.DEVICE).append('\n');
message.append("Host: ").append(android.os.Build.HOST).append('\n');
message.append("ID: ").append(android.os.Build.ID).append('\n');
message.append("Model: ").append(android.os.Build.MODEL).append('\n');
message.append("Product: ").append(android.os.Build.PRODUCT)
.append('\n');
message.append("Type: ").append(android.os.Build.TYPE).append('\n');
StatFs stat = getStatFs();
message.append("Total Internal memory: ")
.append(getTotalInternalMemorySize(stat)).append('\n');
message.append("Available Internal memory: ")
.append(getAvailableInternalMemorySize(stat)).append('\n');
}
public void uncaughtException(Thread t, Throwable e) {
try {
StringBuilder report = new StringBuilder();
Date curDate = new Date();
report.append("Error Report collected on : ")
.append(curDate.toString()).append('\n').append('\n');
report.append("Informations :").append('\n');
addInformation(report);
report.append('\n').append('\n');
report.append("Stack:\n");
final Writer result = new StringWriter();
final PrintWriter printWriter = new PrintWriter(result);
e.printStackTrace(printWriter);
report.append(result.toString());
printWriter.close();
report.append('\n');
report.append("**** End of current Report ***");
Log.e(UncaughtException.class.getName(), "Error while sendErrorMail" + report);
//sendErrorMail(report);
writeToFile(report.toString());
Intent intent = new Intent(context, MainActivity.class);
context.startActivity(intent);
} catch (Throwable ignore) {
Log.e(UncaughtException.class.getName(),
"Error while sending error e-mail", ignore);
}
}
private void writeToFile(String currentStacktrace) {
try {
//Gets the Android external storage directory & Create new folder Crash_Reports
File dir = new File(Environment.getExternalStorageDirectory(), "Crash_Reports");
if (!dir.exists()) {
dir.mkdirs();
}
File file = new File(dir, "Crash_To_Be_Send");
PrintWriter out = null;
if (file.exists() && !file.isDirectory()) {
out = new PrintWriter(new FileOutputStream(file, true));
} else {
//file.createNewFile();
out = new PrintWriter(file);
}
out.append(currentStacktrace);
out.close();
} catch (Exception e) {
Log.e("ExceptionHandler", e.getMessage());
}
//Can i call the below function to post the file and delete the file once posted ?
//postFileToServerAndDeleteFileOncePosted()
}
}
应用程序类代码:
public void onCreate() {
super.onCreate();
Thread.setDefaultUncaughtExceptionHandler(new UncaughtException(this));
}
谁能指出我该如何解决这个问题? 不需要第三方崩溃报告(我已经在其他项目中使用Fabric-Crashlytics)