2017-08-28 90 views
0

我已经设置了一个UncaughtExceptionHandler,以便我的应用程序崩溃时可以将堆栈跟踪写入磁盘。我设置的处理程序是这样的:UncaughtExceptionHandler是否设置了应用程序范围?

if (!(Thread.getDefaultUncaughtExceptionHandler() instanceof CustomExceptionHandler)) { 
     exceptionHandler = new CustomExceptionHandler(
       Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString(), 
       null, this); 

    Thread.setDefaultUncaughtExceptionHandler(exceptionHandler); 
} 

其中CustomExceptionHandler实现UncaughtExceptionHandler。我将实例保存在我的Activity中,因此我可以将它用于其他一些功能(删除堆栈跟踪,检索它们等)。

我在我的ActivityonCreate中调用了上面的那段代码,但它似乎只在第一次运行任何Activity时触发。

我看到Thread.setDefaultUncaughtExceptionHandler调用是静态的,这是否意味着我只能在应用程序中设置该处理程序一次?或者我可以每个线程设置它?

回答

0

从文档

* Sets the default uncaught exception handler. This handler is invoked in 
* case any Thread dies due to an unhandled exception. 

没错,这个处理器是全球性,你需要为每个应用程序

0

是UncaughtExceptionHandler的设置应用广泛,一旦设置呢?

是的。如果将其设置为活动并且活动已被销毁,则活动中的处理程序代码可能不再存在。

我已经在Application-onCreate(不在Activity)中设置处理程序,因此它适用于属于应用程序写入崩溃日志的所有活动。

详见How to change crash message in android(if possible)

Here是遵循GPL V3 +代码为我crashlogger写入logcat的条目文件。

它在Application.onCreate这样

public class AndroFotoFinderApp extends Application { 
    private LogCat mCrashSaveToFile = null; 

    @Override public void onCreate() { 
     super.onCreate(); 

     mCrashSaveToFile = new LogCat(this, Global.LOG_CONTEXT, HugeImageLoader.LOG_TAG, 
       PhotoViewAttacher.LOG_TAG, CupcakeGestureDetector.LOG_TAG, 
       FotoLibGlobal.LOG_TAG, ThumbNailUtils.LOG_TAG, IMapView.LOGTAG, 
       ExifInterface.LOG_TAG, ImageMetaReader.LOG_TAG); 
    } 
} 

其中常数Global.LOG_CONTEXTHugeImageLoader.LOG_TAG ... 是我的代码中使用的不同moduls Android的日志记录标签这样

Log.d(HugeImageLoader.LOG_TAG, "some log message from modul HugeImageLoader) 
初始化
相关问题