您好,感謝您的幫助:如何避免ANR在獨立的Android服務
我想將Java系統到Android,我想將其提供給第三方應用程序通過一個透明的獨立服務,所以它會類似於系統庫。 該系統是一個VoiceXML解釋器,它將解釋由第三方應用程序處理的文檔並將結果發回給它。 這些文件的解釋可能需要很長時間,甚至是很長時間。
現在我有一個服務來創建解釋器來完成所有的工作。我在一個名爲startJVoiceXML()的方法中執行此操作。
問題是我的服務在服務創建後大約20到30秒內被Android與ANR殺死。 但是如果我不在這個方法上做任何繁重的工作(只是這段時間的代碼),服務仍然在運行,並且它不會在更長的時間內被殺。
我是否需要創建一個線程來執行我所需要的操作? 我在代碼中放置了一些註釋以獲得進一步解釋。
謝謝!
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();
}
[你應該通過這個線程](http://stackoverflow.com/questions/8543486/how-to-resolve-the-anr-error-while-invoking-the-camera/8559934#8559934) –