如何避免ANR在独立的Andr​​oid服务 [英] How to avoid ANR in standalone android Service

查看:84
本文介绍了如何避免ANR在独立的Andr​​oid服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,感谢您的帮助:

我想将Java系统到Android,和我想将其提供给第三方应用程序通过一个透明的独立服务,因此将类似的系统库。
该系统是一个相互的VoiceXML preTER至极会由第三方应用程序处理并传回的结果,这间preT文件。
这些文件的跨pretation花费的时间是任意的,甚至很长一段时间。

现在我有一个创建国米preTER,做一切工作的服务。我这样做是在一个叫startJVoiceXML()方法。

问题是,我的服务得到由Android与创建服务约20〜30秒后的ANR杀害。
但是,如果我不这样做的这个方法的任何繁重的工作(只是前一阵的code)该服务仍然运行,并且不会致使在更长的时间内杀死。

我是否需要创建一个线程做什么,我需要?
我把一些评论在code作进一步的解释。

谢谢!

 公共同步无效startJVoiceXML(最终URI URI)抛出JVoiceXMLEvent,InterruptedException的
    {
    AndroidConfiguration配置=新AndroidConfiguration();
    jvxml =新JVoiceXmlMain(配置);
    jvxml.addListener(本);
    jvxml.start();
    INT A = 0;            //等待不是问题,jvxml对象run方法中,关于这一主题做了.notifyAll()的服务调用jvxmlStarted
    this.wait();            //这个一方面是只是为了效仿国米preTER行为做一些长期运行的操作
    而(A< 1000)
    {
        视频下载(500);
        Log.e(JVoiceXML,恩ESTO EL而);
        A = A + 1;
    }    }    公共同步无效jvxmlStarted(){
     this.notifyAll();
    }


解决方案

您应该在一个单独的线程中运行你的CPU密集型code,作为解释的这里


  

一个服务在其宿主进程,该服务的主线程中运行
  不创建它自己的线程并在单独的过程中不执行
  (除非另行指定)。这意味着,如果你的服务是
  打算做任何CPU密集型工作或阻塞操作(如MP3
  回放或网络),你应该创建中的一个新的线程
  服务做的工作。通过使用一个单独的线程,就可以减少
  应用的风险不响应(ANR)错误和
  应用程序的主线程可以继续致力于用户交互
  与你的活动。


Hello and thanks for any help:

I want to port a java system to Android, and I wanna make it available to 3rd party apps through a transparent standalone service so it will resemble a system library. This system is a VoiceXML interpreter wich will interpret documents handled by the 3rd party app and send back the results to it. The interpretation of these documents can take an arbitrary amount of time, even a very long time.

Right now I have a service that creates the Interpreter that does all the work. I do this in a method called startJVoiceXML().

The problem is that my service gets killed by Android with an ANR about 20 to 30 seconds after the service is created. But if I don´t do any heavy work (just the code before the while) on that method the Service remains running and it won´t be killed in a much longer time.

Do I need to create a thread to do what I need to? I place some comments in the code for further explanation.

thanks!

    public synchronized void startJVoiceXML(final URI uri) throws JVoiceXMLEvent, InterruptedException
    {
    AndroidConfiguration config = new AndroidConfiguration();
    jvxml = new JVoiceXmlMain(config);
    jvxml.addListener(this);
    jvxml.start();
    int a=0;

            //the wait is not the problem, the jvxml object run method calls jvxmlStarted in the service that does a .notifyAll() on this thread
    this.wait();    

            //this while is just to "do" some long running operation in order to emulate the Interpreter behaviour
    while(a<1000)
    {
        Thread.sleep(500);
        Log.e("JVoiceXML","esto en el while");
        a=a+1;
    }

    }

    public synchronized void jvxmlStarted() {
     this.notifyAll();
    }

解决方案

You should run your CPU intensive code in a separate thread, as explained here:

A service runs in the main thread of its hosting process—the service does not create its own thread and does not run in a separate process (unless you specify otherwise). This means that, if your service is going to do any CPU intensive work or blocking operations (such as MP3 playback or networking), you should create a new thread within the service to do that work. By using a separate thread, you will reduce the risk of Application Not Responding (ANR) errors and the application's main thread can remain dedicated to user interaction with your activities.

这篇关于如何避免ANR在独立的Andr​​oid服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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