应用程序无响应(ANR)执行服务机器人 [英] Application Not Responding (ANR) executing service android

查看:272
本文介绍了应用程序无响应(ANR)执行服务机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

host.activity是我的包和host.framework.ServicePromemoria是一个Android的服务。

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

这是什么意思的错误??

What does this error mean??

我可以捕获该异常,并避免错误?

Can I catch the exception and avoid error?

推荐答案

这意味着你的服务做一个相当长的操作(大部分是ANR的运营大于5秒),这样做是在UI线程上。这可能是一个网络的任务,或一个数据库的任务,或一些其它长时间操作。

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.

您可以通过运行任务在服务关闭主UI线程,使用的 AsyncTask的

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();

此外,通过高速缓存返回bindservice的价值,如果有的话,如果你需要它供以后使用。

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

这篇关于应用程序无响应(ANR)执行服务机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆