如何在IntentService中运行Android TTS

如何在IntentService中运行Android TTS,android,text-to-speech,logcat,intentservice,Android,Text To Speech,Logcat,Intentservice,我已经看过几个关于从服务中运行TTS的帖子(抱歉,不记得是哪一个),但是没有一个有用 IntentService启动得很好,但在运行onInit()之前它就被破坏了,我认为这就是导致此logcat的原因: 07-28 18:58:24.716 2305-2305/me.arthurtucker.alfredjr D/MainActivity: onPrefChange 07-28 18:58:24.786 2305-2305/me.arthurtucker.alfredjr

我已经看过几个关于从服务中运行TTS的帖子(抱歉,不记得是哪一个),但是没有一个有用

IntentService启动得很好,但在运行onInit()之前它就被破坏了,我认为这就是导致此logcat的原因:

    07-28 18:58:24.716    2305-2305/me.arthurtucker.alfredjr D/MainActivity: onPrefChange
07-28 18:58:24.786    2305-2305/me.arthurtucker.alfredjr D/MainActivity: made new intent
07-28 18:58:24.794    2305-2305/me.arthurtucker.alfredjr D/MainActivity: started service
07-28 18:58:25.044    2305-2305/me.arthurtucker.alfredjr I/SpeachService: onCreate() ran
07-28 18:58:25.255    2305-2573/me.arthurtucker.alfredjr I/TextToSpeech: Sucessfully bound to com.google.android.tts
07-28 18:58:25.255    2305-2573/me.arthurtucker.alfredjr I/SpeachService: onHandleIntent ran
07-28 18:58:25.255    2305-2305/me.arthurtucker.alfredjr W/TextToSpeech: stop failed: not bound to TTS engine
07-28 18:58:25.255    2305-2305/me.arthurtucker.alfredjr W/TextToSpeech: shutdown failed: not bound to TTS engine
07-28 18:58:25.255    2305-2305/me.arthurtucker.alfredjr I/SpeachService: onDestroy() ran
07-28 18:58:25.286    2305-2305/me.arthurtucker.alfredjr E/ActivityThread: Service me.arthurtucker.alfredjr.SpeechService has leaked ServiceConnection android.speech.tts.TextToSpeech$Connection@37ad3e10 that was originally bound here
        android.app.ServiceConnectionLeaked: Service me.arthurtucker.alfredjr.SpeechService has leaked ServiceConnection android.speech.tts.TextToSpeech$Connection@37ad3e10 that was originally bound here
        at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
        at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
        at android.app.ContextImpl.bindService(ContextImpl.java:1437)
        at android.app.ContextImpl.bindService(ContextImpl.java:1426)
        at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
        at android.speech.tts.TextToSpeech.connectToEngine(TextToSpeech.java:627)
        at android.speech.tts.TextToSpeech.initTts(TextToSpeech.java:597)
        at android.speech.tts.TextToSpeech.<init>(TextToSpeech.java:553)
        at android.speech.tts.TextToSpeech.<init>(TextToSpeech.java:527)
        at android.speech.tts.TextToSpeech.<init>(TextToSpeech.java:512)
        at me.arthurtucker.alfredjr.SpeechService.onHandleIntent(SpeechService.java:37)
        at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.os.HandlerThread.run(HandlerThread.java:60)
07-28 18:58:24.716 2305-2305/me.arthurtucker.alfredjr D/main活动:onPrefChange
07-28 18:58:24.786 2305-2305/me.arthurtucker.alfredjr D/main活动:制定新的意图
07-28 18:58:24.794 2305-2305/me.arthurtucker.alfredjr D/main活动:开始服务
07-28 18:58:25.044 2305-2305/me.arthurtucker.alfredjr I/SpeachService:onCreate()运行
07-28 18:58:25.255 2305-2573/me.arthurtucker.alfredjr I/TextToSpeech:成功绑定到com.google.android.tts
07-28 18:58:25.255 2305-2573/me.arthurtucker.alfredjr I/SpeachService:onHandleIntent run
07-28 18:58:25.255 2305-2305/me.arthurtucker.alfredjr W/text语音:停止失败:未绑定到TTS引擎
07-28 18:58:25.255 2305-2305/me.arthurtucker.alfredjr W/text语音:关机失败:未绑定到TTS引擎
07-28 18:58:25.255 2305-2305/me.arthurtucker.alfredjr I/SpeachService:onDestroy()运行
07-28 18:58:25.286 2305-2305/me.arthurtucker.alfredjr E/ActivityThread:Service me.arthurtucker.alfredjr.SpeechService已泄漏服务连接android.speech.tts.TextToSpeech$Connection@37ad3e10原来是订在这里的
android.app.ServiceConnection泄漏:Service me.arthurtucker.alfredjr.SpeechService已泄漏ServiceConnection android.speech.tts.TextToSpeech$Connection@37ad3e10原来是订在这里的
在android.app.LoadedApk$ServiceDispatcher上。(LoadedApk.java:969)
位于android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
位于android.app.ContextImpl.bindService(ContextImpl.java:1437)
位于android.app.ContextImpl.bindService(ContextImpl.java:1426)
位于android.content.ContextWrapper.bindService(ContextWrapper.java:473)
位于android.speech.tts.TextToSpeech.connectToEngine(TextToSpeech.java:627)
位于android.speech.tts.TextToSpeech.initTts(TextToSpeech.java:597)
在android.speech.tts.TextToSpeech.(TextToSpeech.java:553)
在android.speech.tts.TextToSpeech.(TextToSpeech.java:527)
在android.speech.tts.TextToSpeech。(TextToSpeech.java:512)
atme.arthurtucker.alfredjr.SpeechService.onHandleIntent(SpeechService.java:37)
位于android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
位于android.os.Handler.dispatchMessage(Handler.java:99)
位于android.os.Looper.loop(Looper.java:137)
运行(HandlerThread.java:60)
我已经尝试在onCreate()、onHandleContent()和现在的onInit()中运行sayString()。在onCreate()或onHandleContent()中,logcat会报告类似这样的内容:“讲话失败:未绑定到tts引擎”

有没有办法让IntentService在销毁之前等待onInit()运行

我的代码如下

    public class SpeechService extends IntentService implements TextToSpeech.OnInitListener, TextToSpeech.OnUtteranceCompletedListener {
    private static TextToSpeech myTTS;
    private Main mActivity;
    private static final String TAG = "SpeachService";
    //private String msg;

    public SpeechService() {
        super("SpeechService");
    }

    @Override
    public void onCreate() {
        mActivity = new Main();
        super.onCreate();
        Log.i(TAG, "onCreate() ran");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Intent checkTTSIntent = new Intent();
        checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        myTTS = new TextToSpeech(this, this);
        //String msg = intent.getStringExtra("imsg");
        Log.i(TAG, "onHandleIntent ran");
    }

    public void sayString(String string) {
        if (myTTS != null) {
            if (mActivity.mEnabled) {
                HashMap<String, String> hashMap = new HashMap<String, String>();
                hashMap.put(TextToSpeech.Engine.KEY_FEATURE_NETWORK_SYNTHESIS, "true");
                myTTS.speak(string, 1, hashMap);
            } else {
                myTTS.speak(string, 1, null);
            }
        }

        Log.i(TAG, "sayString() ran");
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        if (myTTS != null) {
            myTTS.stop();
            myTTS.shutdown();
        }
        super.onDestroy();
        Log.i(TAG, "onDestroy() ran");
    }

    @Override
    public void onInit(int initStatus) {
        if (initStatus == TextToSpeech.SUCCESS) {
            if (myTTS.isLanguageAvailable(Locale.UK) == TextToSpeech.LANG_AVAILABLE) {
                myTTS.setLanguage(Locale.UK);
            }
            sayString("IT WORKS");
        } else if (initStatus == TextToSpeech.ERROR) {
            mActivity.makeToast("Failed to start Text-to-Speech Service", true);
        }
        Log.i(TAG, "onInit() ran");
    }

    @Override
    public void onUtteranceCompleted(String s) {
        stopSelf();
        Log.i(TAG, "onUtteranceCompleted() ran");
    }
}
公共类SpeechService扩展IntentService实现TextToSpeech.OnInitListener、TextToSpeech.OnutternanceCompletedListener{
私有静态文本来表示myTTS;
私人主活动;
私有静态最终字符串TAG=“SpeachService”;
//私有字符串msg;
公共演讲服务{
超级(“演讲服务”);
}
@凌驾
public void onCreate(){
mActivity=newmain();
super.onCreate();
i(标记“onCreate()ran”);
}
@凌驾
受保护的手部内容无效(意图){
Intent checkttsinent=新Intent();
checkttsinent.setAction(TextToSpeech.Engine.ACTION\u CHECK\u TTS\u数据);
myTTS=新文本到语音(这个,这个);
//String msg=intent.getStringExtra(“imsg”);
Log.i(标记“onHandleIntent ran”);
}
公共空字符串(字符串){
如果(myTTS!=null){
if(mActivity.mEnabled){
HashMap HashMap=新的HashMap();
hashMap.put(TextToSpeech.Engine.KEY\u FEATURE\u NETWORK\u SYNTHESIS,“true”);
speak(字符串,1,hashMap);
}否则{
myTTS.speak(字符串,1,null);
}
}
i(标记“sayString()ran”);
}
@凌驾
公共空间{
//TODO自动生成的方法存根
如果(myTTS!=null){
myTTS.stop();
myTTS.shutdown();
}
super.ondestory();
i(标记“onDestroy()ran”);
}
@凌驾
public void onInit(int initStatus){
if(initStatus==TextToSpeech.SUCCESS){
if(myTTS.islanguage可用(Locale.UK)=TextToSpeech.LANG\u可用){
myTTS.setLanguage(Locale.UK);
}
sayString(“它起作用”);
}else if(initStatus==TextToSpeech.ERROR){
mActivity.makeToast(“无法启动文本到语音服务”,true);
}
i(标记“onInit()ran”);
}
@凌驾
已完成公用void操作(字符串s){
stopSelf();
i(标记“onutternancecompleted()ran”);
}
}

我愿意尝试完全不同的方法,只要它做的事情相似。

您没有显式调用
onInit()
IntentService
一旦调用
onHandleIntent
方法就会被销毁。请确保您在
onHandleIntent
方法中完成所有工作

文档中的onHandleIntent(意图):

此方法在具有处理请求的工作线程上调用。一次只处理一个意图,但处理发生在独立于其他应用程序逻辑运行的工作线程上。因此,如果此代码花费很长时间,它将保留对同一个IntentService的其他请求,但不会保留任何内容se.当所有请求都已处理完毕时