Avoiding ANR in Android Standalone Service

Hi and thanks for any help:

I want to port the java system to Android, and I want to make it available for third-party applications through a transparent standalone service so that it looks like a system library. This system is a VoiceXML interpreter that will interpret documents processed by a third-party application and send the results back to it. The interpretation of these documents can take an arbitrary amount of time, even a very long time.

Now I have a service that creates an interpreter that does all the work. I do this in the startJVoiceXML () method.

The problem is that my service was killed by Android with ANR about 20-30 seconds after creating the service. But if I do not do the hard work (only the code until this time), this service remains on, and it will not be killed for a much longer time.

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

thank!

    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();
    }
+2
source share
1 answer

You must run your processor intensive code in a separate thread, as described here :

- ( ). , (, MP3 ), . , (ANR) .

+2

All Articles