Skip to content

Commit

Permalink
feat: Do not use AlarmManager for uncaught exception handler
Browse files Browse the repository at this point in the history
  • Loading branch information
aikrq committed Nov 3, 2024
1 parent e6dd12a commit a4a56a7
Showing 1 changed file with 9 additions and 19 deletions.
28 changes: 9 additions & 19 deletions app/src/main/java/pro/sketchware/SketchApplication.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package pro.sketchware;

import android.app.AlarmManager;
import android.app.Application;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Process;
Expand All @@ -13,7 +11,6 @@
import mod.trindadedev.manage.theme.ThemeManager;

public class SketchApplication extends Application {

private static Context mApplicationContext;

public static Context getContext() {
Expand All @@ -23,22 +20,15 @@ public static Context getContext() {
@Override
public void onCreate() {
mApplicationContext = getApplicationContext();
Thread.UncaughtExceptionHandler uncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler((thread, throwable) -> {
Log.e("SketchApplication", "Uncaught exception on thread " + thread.getName(), throwable);

Intent intent = new Intent(getApplicationContext(), CollectErrorActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.putExtra("error", Log.getStackTraceString(throwable));
((AlarmManager) getSystemService(Context.ALARM_SERVICE))
.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
1000,
PendingIntent.getActivity(getApplicationContext(), 11111, intent,
PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE));
Process.killProcess(Process.myPid());
System.exit(1);
if (uncaughtExceptionHandler != null) {
uncaughtExceptionHandler.uncaughtException(thread, throwable);
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable throwable) {
Intent intent = new Intent(getApplicationContext(), CollectErrorActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.putExtra("error", Log.getStackTraceString(throwable));
startActivity(intent);
Process.killProcess(Process.myPid());
System.exit(1);
}
});
super.onCreate();
Expand Down

0 comments on commit a4a56a7

Please sign in to comment.