gpt4 book ai didi

android - 如何实现 uncaughtException android

转载 作者:IT老高 更新时间:2023-10-28 21:50:35 28 4
gpt4 key购买 nike

我发现了这个 Android: How to auto-restart application after it's been "force closed"?

但我不知道警报管理器应该放在哪里以及如何放置

谢谢

最佳答案

您可以在应用程序扩展类中捕获所有未捕获的异常。在异常处理程序中对异常执行一些操作并尝试设置 AlarmManager 以重新启动您的应用程序。这是我如何在我的应用程序中执行此操作的示例,但我只将异常记录到数据库。

public class MyApplication extends Application {
// uncaught exception handler variable
private UncaughtExceptionHandler defaultUEH;

// handler listener
private Thread.UncaughtExceptionHandler _unCaughtExceptionHandler =
new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable ex) {

// here I do logging of exception to a db
PendingIntent myActivity = PendingIntent.getActivity(getContext(),
192837, new Intent(getContext(), MyActivity.class),
PendingIntent.FLAG_ONE_SHOT);

AlarmManager alarmManager;
alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
15000, myActivity );
System.exit(2);

// re-throw critical exception further to the os (important)
defaultUEH.uncaughtException(thread, ex);
}
};

public MyApplication() {
defaultUEH = Thread.getDefaultUncaughtExceptionHandler();

// setup handler for uncaught exception
Thread.setDefaultUncaughtExceptionHandler(_unCaughtExceptionHandler);
}
}

关于android - 如何实现 uncaughtException android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8943288/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com