Skip to content

Instantly share code, notes, and snippets.

@denihida1216
Created April 19, 2022 00:50
Show Gist options
  • Save denihida1216/9d9c526adfb9edc5d618ef5d7bc6fc3e to your computer and use it in GitHub Desktop.
Save denihida1216/9d9c526adfb9edc5d618ef5d7bc6fc3e to your computer and use it in GitHub Desktop.
handler error not force close java android studio
package com.example.mobile.helper;
import java.io.PrintWriter;
import java.io.StringWriter;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import com.example.mobile.activity.HandlingErrorActivity;
public class ExceptionHandler implements
java.lang.Thread.UncaughtExceptionHandler {
private final Activity myContext;
private final String LINE_SEPARATOR = "\n";
public ExceptionHandler(Activity context) {
myContext = context;
}
public void uncaughtException(Thread thread, Throwable exception) {
StringWriter stackTrace = new StringWriter();
exception.printStackTrace(new PrintWriter(stackTrace));
StringBuilder errorReport = new StringBuilder();
errorReport.append("************ CAUSE OF ERROR ************\n\n");
errorReport.append(stackTrace.toString());
errorReport.append("\n************ DEVICE INFORMATION ***********\n");
errorReport.append("Brand: ");
errorReport.append(Build.BRAND);
errorReport.append(LINE_SEPARATOR);
errorReport.append("Device: ");
errorReport.append(Build.DEVICE);
errorReport.append(LINE_SEPARATOR);
errorReport.append("Model: ");
errorReport.append(Build.MODEL);
errorReport.append(LINE_SEPARATOR);
errorReport.append("Id: ");
errorReport.append(Build.ID);
errorReport.append(LINE_SEPARATOR);
errorReport.append("Product: ");
errorReport.append(Build.PRODUCT);
errorReport.append(LINE_SEPARATOR);
errorReport.append("\n************ FIRMWARE ************\n");
errorReport.append("SDK: ");
errorReport.append(Build.VERSION.SDK);
errorReport.append(LINE_SEPARATOR);
errorReport.append("Release: ");
errorReport.append(Build.VERSION.RELEASE);
errorReport.append(LINE_SEPARATOR);
errorReport.append("Incremental: ");
errorReport.append(Build.VERSION.INCREMENTAL);
errorReport.append(LINE_SEPARATOR);
Intent intent = new Intent(myContext, HandlingErrorActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_CLEAR_TASK |
Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("logstr", errorReport.toString());
intent.putExtra("brand", Build.BRAND);
intent.putExtra("device", Build.DEVICE);
intent.putExtra("model", Build.MODEL);
intent.putExtra("productid", Build.ID);
intent.putExtra("product", Build.PRODUCT);
intent.putExtra("sdk", Build.VERSION.SDK);
intent.putExtra("release", Build.VERSION.RELEASE);
intent.putExtra("incremental", Build.VERSION.INCREMENTAL);
myContext.startActivity(intent);
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(10);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment