Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application Not Responding (ANR) executing service android

error service

host.activity is my package and host.framework.ServicePromemoria is an android service.

What does this error mean?

like image 244
GVillani82 Avatar asked Oct 24 '12 16:10

GVillani82


People also ask

What causes ANR error Android?

Application Not Responding (ANR) errors are triggered when the UI thread of the application is not responding for more than 5 seconds. You can read more about ANRs and diagnosing ANRs in the Android documentation.

What is ANR and how do you prevent it?

In Android, application responsiveness is monitored by the Activity Manager and Window Manager system services. Android will display the ANR dialog for a particular application when it detects one of the following conditions: No response to an input event (such as key press or screen touch events) within 5 seconds.

What is ANR in Google Play store?

If your app stops responding, users get a dialog that allows them to wait or close the app. When these dialogs appear, they're known as "Application not responding" errors (or ANRs). ANR data is available in Play Console only.


2 Answers

This means that your service is doing a rather long operation (most ANRs are from operations greater than 5 seconds) and is doing it on the UI thread. This could be a network task, or a database task, or some other long operation.

You can fix this by running the task in a service off the main UI thread, by using a Thread or an AsyncTask.

Infact, you can directly start your service into a new thread as follows:

Thread t = new Thread(){
public void run(){
getApplicationContext().bindService(
        new Intent(getApplicationContext(), YourService.class),
        serviceConnection,
        Context.BIND_AUTO_CREATE
    );
}
};
t.start();

Also, cache the value returned by bindservice, if any, if you require it for later use.

like image 119
Raghav Sood Avatar answered Oct 04 '22 23:10

Raghav Sood


Although there is an accepted answer but I want do add something additional here .The reason "Application Not Responding (ANR) executing service android" is that your Service does not finished it's lifecycle call (like onCreate) during the timeout . and the default timeout for a normal service is 20s . if you need an block operate in a Servcie onCreate you need start a new Thread or you can just use the IntentService .

like image 44
Usher Avatar answered Oct 05 '22 01:10

Usher