我希望用户能够在我的服务崩溃时发送错误报告。我有一个GUI应用程序,它使用来自服务的广播进行更新。该服务在不同的进程中运行并作为前台运行。我使用相同的代码将默认的异常处理程序附加到我的GUI,并在那里工作正常(打开电子邮件发送应用程序和电子邮件的正文包含异常)。但是对于我的服务线程,我无法让它们调用UncaughtExceptionHandler。
我到目前为止所做的研究是,崩溃的线程有一个不同的线程(12),而不是我在(229)上注册了cutom exceptionhandler的线程。注册和崩溃是在同一个Timer_Tick可运行的,应该有相同的线程ID。
Logcat输出:
> D/Registratie: General exception handler set for threadid=229
> D/Registratie: ThreadName in Timer_Tick: Timer-0 threadId=229
> D/Registratie: ThreadName in Timer_Tick: Timer-0 threadId=229
> D/Registratie: ThreadName in Timer_Tick: Timer-0 threadId=229
> D/Registratie: Throw ArrayIndexOutOfBounds exception W/dalvikvm:
> threadid=12: thread exiting with uncaught exception (group=0x4169fba8)
服务成员和方法:
// declared non-anonymous to prevent the garbage collector erroneously clearing
private Thread.UncaughtExceptionHandler mUEHandler;
public void attachGeneralExceptionHandler(){
mUEHandler = new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
Log.d(TAG, "Custom crash handler: build crashrapport and intent");
sendExceptionReport(t,e);
mUEHandler.uncaughtException(t, e);
}
};
Thread.setDefaultUncaughtExceptionHandler(mUEHandler);
Log.d(TAG, "General exception handler set for ThreadName: " + Thread.currentThread().getName() + " threadid=" + Thread.currentThread().getId());
}
来自服务的TimerTick:
private Runnable Timer_Tick = new Runnable() {
public void run() {
if(!uncaughtExceptionHandlerSet) {
// Make sure the exception handler is connected to this Timer_Tick thread
attachGeneralExceptionHandler();
uncaughtExceptionHandlerSet=true;
}
Log.d(TAG, "ThreadName in Timer_Tick: "+ Thread.currentThread().getName()+" threadId="+Thread.currentThread().getId());
if(testExceptionHandling){
Log.d("TAG", "Throw ArrayIndexOutOfBounds exception");
int[] exceptionTest = new int[3];
exceptionTest[3] = -1; // throws exception on thread with threadid 12, only one line in logcat
}
}
答案 0 :(得分:0)
The program '[2884] WaIISHost.exe: Program Trace' has exited with code 0 (0x0).
The program '[2884] WaIISHost.exe' has exited with code 0 (0x0).
The program '[12316] w3wp.exe' has exited with code 0 (0x0).
The program '[12316] w3wp.exe: Program Trace' has exited with code 0 (0x0).
的{{3}}表示:
如果任何线程由于a而死亡,则会调用此处理程序 未处理的异常
您不需要为每个线程调用该方法。尝试在服务的Thread.setDefaultUncaughtExceptionHandler()
方法中设置默认句柄。