gpt4 book ai didi

Android UncaughtExceptionHandler 不工作

转载 作者:太空狗 更新时间:2023-10-29 14:10:44 25 4
gpt4 key购买 nike

我正在创建一个启动器。有时我会遇到错误,当然我必须修复它们,但我正在尝试为我的整个应用程序使用 UncaughtExceptionHandler。

为此我使用了这个类:

  public class clsMyApplication 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)
{
try
{ Save a log file with the error and
save preferences indicating there was an error to display the next start up.
}
catch(Exception e)
{

}



//Alarm to restart app:
PendingIntent myActivity = PendingIntent.getActivity(clsMyApplication.this, 192837, new Intent(clsMyApplication.this, clsMyMainActivity.class), PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 5000, myActivity );
System.exit(2);


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

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

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


}

我还像这样将此类添加到 list 中:

<application       
android:name=".clsMyApplication"
....

但我仍然遇到 Android 异常。我无法让他们使用此类。有帮助吗?

最佳答案

将这行代码添加到任何方法中(最好是在 OnCreate Activity 中)

Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {

@Override
public void uncaughtException(Thread thread, Throwable t) {
//Do something
}
});

关于Android UncaughtExceptionHandler 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28836600/

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