如何在BroadcastReceiver应用程序中添加uncaughtExceptionHandler函数?

时间:2013-12-12 08:32:21

标签: android

我知道我可以将以下代码添加到onCreate of Activity来处理uncaughtException。 但在我的BroadcastReceiver应用程序中,我没有UI,也没有Activity,我该怎么办?

final UncaughtExceptionHandler defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
    @Override
    public void uncaughtException(Thread thread, Throwable ex) {        
        //TODO Handle Exception
        defaultUEH.uncaughtException(thread, ex);
    }
});

1 个答案:

答案 0 :(得分:1)

尝试将此代码放入BroadcastReceiver

的构造函数中
public MyBroadcastReceiver() {
    final UncaughtExceptionHandler defaultUEH = Thread
            .getDefaultUncaughtExceptionHandler();
    Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
        @Override
        public void uncaughtException(Thread thread, Throwable ex) {
            // TODO Handle Exception
            defaultUEH.uncaughtException(thread, ex);
        }
    });
}