What is ANR? How to avoid and resolve it?

Application Not Responding, that is, the application has no response.
There are generally three types of ANR:
1: KeyDispatchTimeout (5 seconds) the main type of
key or touch event is not responding within a specific time

2: Broadcast Timeout (10 seconds)
BroadcastReceiver cannot be processed within a certain time

3: Service Timeout (20 seconds) - small probability
service cannot be completed within a certain time

There are generally two reasons for timeout:
(1) the current event has no chance to be processed (the UI thread is processing the previous event did not complete in time or the looper is blocked for some reason)
(2) the current event is being processed, but not in time carry out

The UI thread tries to do only UI-related work and time-consuming work (database operations, I/O, network connection, or other operations that may hinder the UI thread). Put it in a separate thread. Use Handler to handle UI thread and thread Interaction.

UI threads mainly include the following:
Activity: onCreate (), onResume (), onDestroy (), onKeyDown (), onClick ()
AsyncTask: onPreExecute (), onProgressUpdate (), onPostExecute (), onCancel ()
Mainthread handler: handleMessage () , post (runnable r)
other

Application Not Responding, that is, the application has no response.
There are generally three types of ANR:
1: KeyDispatchTimeout (5 seconds)-the main type of
key or touch event is not responding within a specific time

2: BroadcastTimeout (10 seconds)
BroadcastReceiver cannot be processed within a certain time

3: ServiceTimeout (20 seconds)-small probability
service cannot be completed within a certain time

There are generally two reasons for timeout:
(1) the current event has no chance to be processed (the UI thread is processing the previous event did not complete in time or the looper is blocked for some reason)
(2) the current event is being processed, but not in time carry out

The UI thread tries to do only UI-related work and time-consuming work (database operations, I/O, network connection, or other operations that may hinder the UI thread). Put it in a separate thread. Use Handler to handle UI thread and thread Interaction.

UI threads mainly include the following:
Activity: onCreate (), onResume (), onDestroy (), onKeyDown (), onClick ()
AsyncTask: onPreExecute (), onProgressUpdate (), onPostExecute (), onCancel ()
Main thread handler: handleMessage () , post (runnable r)
other

ANR stands for application is not responding, it is a dialog box that appears when the application is not responding.

Android Applications run on the Main thread when any task takes more than 5 seconds to do his job on the UI thread, then the Android system will show this dialog ANR.