在Android应用程序中,如果我们没有得到正确的例外,我们通常会收到"强制关闭"错误.
如果强行关闭,如何自动重启我的应用程序?
是否有任何特定的权限用于此?
我正在尝试为我的应用程序编写一个单元测试未被捕获的线程异常处理程序,但到目前为止还没有运气.处理程序是应用程序范围的,我知道它的工作原理.它也基于此处的代码.但是我想不出一种实际为它编写单元测试的方法,考虑到如果我从我的测试项目中抛出一个异常就输入了那个代码,但它永远不会返回.这导致我到目前为止所写的任何测试都失败了.
这是我的处理程序,有没有人有任何建议如何我可以单元测试这个?
public class MyApplication extends Application {
// uncaught exception handler variable
private final UncaughtExceptionHandler defaultUEH;
// handler listener
private final Thread.UncaughtExceptionHandler _unCaughtExceptionHandler = new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable ex) {
// Try restarting the application if an uncaught exception has
// occurred.
PendingIntent myActivity = PendingIntent
.getActivity(getApplicationContext(), 192837, new Intent(
getApplicationContext(), MainGui.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) …Run Code Online (Sandbox Code Playgroud)