Application Not Responding (ANR) executing service android

為{幸葍}努か 提交于 2019-12-07 17:38:58

问题


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

What does this error mean?


回答1:


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.




回答2:


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 .



来源:https://stackoverflow.com/questions/13053611/application-not-responding-anr-executing-service-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!