问题
I used to handle exception errors with this class and pass error as String to another activity for show , but this way not work any more , seems when kill process happened "sag.class" Activity cant launch any more
public class ExceptionHandler extends MainActivity implements java.lang.Thread.UncaughtExceptionHandler {
private final Context myContext;
private SharedPreferences prefs;
private StringBuilder errorReport;
public ExceptionHandler(Context context) {
myContext = context;
}
public void uncaughtException(Thread thread, Throwable exception) {
StringWriter stackTrace = new StringWriter();
exception.printStackTrace(new PrintWriter(stackTrace));
System.err.println(stackTrace);
exception.printStackTrace(new PrintWriter(stackTrace));
errorReport = new StringBuilder();
errorReport.append("************ CAUSE OF ERROR ************\n\n");
errorReport.append(stackTrace.toString());
errorReport.append("\n************ DEVICE INFORMATION ***********\n");
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);
errorReport.append("App Version: ");
try {
errorReport.append(myContext.getApplicationContext().getPackageManager().getPackageInfo(myContext.getPackageName(), 0).versionName);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
errorReport.append(LINE_SEPARATOR);
prefs = myContext.getSharedPreferences("MyPrefsFile", MODE_PRIVATE);
if (prefs.contains("access_token"))
{
errorReport.append(prefs.getString("fname","")+" "+prefs.getString("lname",""));
}
Intent intent = new Intent(myContext, sag.class);
intent.putExtra("error", errorReport.toString());
myContext.startActivity(intent);
Process.killProcess(Process.myPid());
System.exit(10);
}
}
so how can start activity in UncaughtExceptionHandler class with new way ?
回答1:
try this, please add this code to start new activity
Intent startAppIntent = new Intent(this, sag.class);
startAppIntent.putExtra("error", errorReport.toString());
startAppIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startAppIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startAppIntent);
System.exit(1);
you can change this
to myContext
if this
doesn't work. Please let us know if this code works for you.
回答2:
Short Answer : Yes you can start an activity from UncaughtExceptionHandler
.
fun restartApp(context : Context) {
val intent = Intent(context, Destination::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(intent)
android.os.Process.killProcess(android.os.Process.myPid())
exitProcess(0)
}
you can also find the exit status number in this link
来源:https://stackoverflow.com/questions/55204957/start-activity-in-custom-uncaughtexceptionhandler-class-not-working-any-more