The system displays an ANR if an application cannot respond to user input.
* What Triggers ANR?
Application responsiveness is monitored by the Activity Manager and Window Manager system services.
- No response to an input event (e.g. key press, screen touch) within 5 seconds
- A BroadcastReceiver hasn't finished executing within 10 seconds
* Android applications normally run entirely on a single (i.e. main) thread.
This means that anything your application is doing in the main thread that takes a long time to complete can trigger the ANR dialog because your application is not giving itself a chance to handle the input event or Intent broadcast.
[How to Avoid ANR]
* The recommended approach is to create a child thread and do most of your work there.
- This keeps the main thread (which drives the user interface event loop) running and prevents the system from concluding that your code has frozen.
- Since such threading usually is accomplished at the class level.
* Potentially long running operations (ex: network or database operations, or computationally expensive calculations such as resizing bitmaps) should be done in a child thread (or in the case of databases operations, via an asynchronous request). However, this does not mean that your main thread should block while waiting for the child thread to complete.
* Should also avoid starting an Activity from an Intent Receiver (it will spawn a new screen that will steal focus from whatever application the user is currently has running). If your application has something to show the user in response to an Intent broadcast, it should do so using the Notification Manager.
* Reference
- Designing for Responsiveness
沒有留言:
張貼留言