### Start ANR-WatchDog in Application Source: https://github.com/salomonbrys/anr-watchdog/blob/master/README.md Initialize and start the ANRWatchDog in your application's onCreate method to begin monitoring for ANRs. ```java new ANRWatchDog().start(); ``` -------------------------------- ### Example ANRError Stack Trace Source: https://github.com/salomonbrys/anr-watchdog/blob/master/README.md This is an example of an ANRError stack trace, illustrating the structure of reports generated by the ANR WatchDog. Each 'Caused by' section represents the stack trace of a different thread. ```java FATAL EXCEPTION: |ANR-WatchDog| Process: anrwatchdog.github.com.testapp, PID: 26737 com.github.anrwatchdog.ANRError: Application Not Responding Caused by: com.github.anrwatchdog.ANRError$_$_Thread: main (state = WAITING) at testapp.MainActivity$1.run(MainActivity.java:46) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) Caused by: com.github.anrwatchdog.ANRError$_$_Thread: APP: Locker (state = TIMED_WAITING) at java.lang.Thread.sleep(Native Method) at java.lang.Thread.sleep(Thread.java:1031) at java.lang.Thread.sleep(Thread.java:985) at testapp.MainActivity.SleepAMinute(MainActivity.java:18) at testapp.MainActivity.access$100(MainActivity.java:12) at testapp.MainActivity$LockerThread.run(MainActivity.java:36) ``` -------------------------------- ### Set Thread Name for Reporting Source: https://github.com/salomonbrys/anr-watchdog/blob/master/README.md Example of how to set a thread's name to include the specified prefix ('APP:') so that it will be reported by the ANR WatchDog when filtering is enabled. ```java public class MyAmazingThread extends Thread { @Override public void run() { setName("APP: Amazing!"); /* ... do amazing things ... */ } } ``` -------------------------------- ### ANR Interceptor Example Source: https://github.com/salomonbrys/anr-watchdog/blob/master/README.md Define an ANRInterceptor to control ANR reporting based on freeze duration. The interceptor can postpone ANR errors by returning a positive value representing the additional delay in milliseconds. ```java new ANRWatchDog(2000).setANRInterceptor(new ANRWatchDog.ANRInterceptor() { @Override public long intercept(long duration) { long ret = 5000 - duration; if (ret > 0) { Log.w(TAG, "Intercepted ANR that is too short (" + duration + " ms), postponing for " + ret + " ms."); } return ret; } }) ``` -------------------------------- ### Filter Reported Threads by Prefix Source: https://github.com/salomonbrys/anr-watchdog/blob/master/README.md Configure the ANR WatchDog to only report threads whose names start with a specified prefix. This helps in filtering out system threads and focusing on application-specific threads. ```java new ANRWatchDog().setReportThreadNamePrefix("APP:").start(); ``` -------------------------------- ### Set Custom ANR Timeout Source: https://github.com/salomonbrys/anr-watchdog/blob/master/README.md Configure the ANR WatchDog to use a custom timeout duration. This example sets the timeout to 10000 milliseconds (10 seconds). Ensure this is only applied in debug builds. ```java if (BuildConfig.DEBUG == false) { new ANRWatchDog(10000 /*timeout*/).start(); } ``` -------------------------------- ### Add ANR-WatchDog Dependency Source: https://github.com/salomonbrys/anr-watchdog/blob/master/README.md Include this dependency in your app's build.gradle file to use ANR-WatchDog. ```gradle implementation 'com.github.anrwatchdog:anrwatchdog:1.4.0' ``` -------------------------------- ### Custom ANR Callback Source: https://github.com/salomonbrys/anr-watchdog/blob/master/README.md Implement a custom ANRListener to handle ANR events without crashing the application. This is recommended for production environments to log errors instead of terminating the app. ```java new ANRWatchDog().setANRListener(new ANRWatchDog.ANRListener() { @Override public void onAppNotResponding(ANRError error) { // Handle the error. For example, log it to HockeyApp: ExceptionHandler.saveException(error, new CrashManager()); } }).start(); ``` -------------------------------- ### Report Only Main Thread Source: https://github.com/salomonbrys/anr-watchdog/blob/master/README.md Configure the ANR WatchDog to only include the main thread's stack trace in the ANRError report, excluding all other threads. ```java new ANRWatchDog().setReportMainThreadOnly().start(); ``` -------------------------------- ### Ignore Debugger Detection Source: https://github.com/salomonbrys/anr-watchdog/blob/master/README.md Disable the ANR WatchDog's default behavior of ignoring ANRs when a debugger is attached. This allows ANRError to be thrown even when debugging. ```java new ANRWatchDog().setIgnoreDebugger(true).start(); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.