Giter Club home page Giter Club logo

anr-watchdog's Introduction

ANR-WatchDog

A simple watchdog that detects Android ANR (Application Not Responding) error and throws a meaningful exception

Why it exists

There is currently no way for an android application to catch and report ANR errors. If your application is not in the play store (either because you are still developing it or because you are distributing it differently), the only way to investigate an ANR is to pull the file /data/anr/traces.txt and try to navigate your way in this enormous file.

There is an issue entry in the android bug tracker describing this lack, feel free to star it ;)

What it does

It sets up a watchdog that will detect when the UI thread stops responding. When it does, it raises an exception with the UI thread stack trace.

Can it work with crash reporters like ACRA ?

Yes ! I'm glad you ask : That's the reason why it was developped in the first place ! As this throws an exception, a crash handler can intercept it and handle it the way it needs.

How it works

The watchdog is a simple thread that :

  1. Schedule small code to be run on the UI thread as soon as possible.
  2. Wait for 5 seconds. (5 seconds is the default, but it can be configured).
  3. See if the code has run : if it has, go back to 1
  4. If the code has not run, it means that the UI thread has been blocked for at least 5 seconds, raises an exception with the UI thread stack trace

How to use it

  1. Download the jar

  2. Put ANRWatchDog.jar in the libs/ directory of your project

  3. In your application class, add an ANRWatchDog field:

    public ANRWatchDog watchDog = new ANRWatchDog();
    

    Note that you can configure the watchdog interval (5000 miliseconds by default). For example, if you want to have a 10 seconds interval:

    public ANRWatchDog watchDog = new ANRWatchDog(10000);
    
  4. In your application class, in onCreate, add:

    if (BuildConfig.DEBUG == false)
        watchDog.start();
    

    This only enable the watchdog when not debugging. That's because the watchdog will prevent the Android java debugger from hanging execution at breakpoints or exceptions (it will detect the debugging pause as an ANR).

  5. You're done

Advanced use

  • ANRWatchDog is a thread, so you can interrupt it at any time.

  • If you are programming with Android's multi process capability (like starting an activity in a new thread), remember that you will need an ANRWatchDog thread per process

anr-watchdog's People

Contributors

ozmium avatar salomonbrys avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.