简体   繁体   English

android:TextToSpeech泄漏服务连接

[英]android: TextToSpeech leaking service connection

I am trying to create an app using text to speech. 我正在尝试使用文字转语音创建应用。 Whenever I move from activity Text_entry to activity CombChange and back i receive these service connection leaked errors in the log cat (below). 每当我从活动Text_entry移到活动CombChange并返回时,我都会在日志猫中收到这些服务连接泄漏的错误(如下)。

Can anyone see why? 谁能看到原因?

I have only pasted the relevant bits of the Text_Entry and CombChange classes - so there may be missing declarations and curly brackets etc. 我只粘贴了Text_Entry和CombChange类的相关位-因此可能缺少声明和大括号等。

public class Text_entry extends Activity implements OnTouchListener, TextToSpeech.OnInitListener{

    speech = new Speech(this);
        Intent checkTTSIntent = new Intent();
        checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE);

    Intent intent2 = new Intent(this, CombChange.class);
    intent2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent2.putExtra("newPat", enteredNumber);
    startActivity(intent2);
}

@Override
    public void onResume() {
        super.onResume();
        Intent intent = getIntent();
        String hc =" ";
        hc = intent.getExtras().getString("helpComb");
        if (hc.equals("true"))
            helpComb = true;
        else
            helpComb = false;
    }

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == MY_DATA_CHECK_CODE) {
            if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
                speech = new Speech(this);
            }
            else {
                Intent installTTSIntent = new Intent();
                installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                startActivity(installTTSIntent);
            }
            }
    }

.

public class CombChange extends ListActivity {
    speech = new Speech(this);
    speech.changeText("enter the new combination");
    speech.speaks();
    SystemClock.sleep(1300);
        Intent intent = new Intent(this, Text_entry.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putExtra("helpComb", "true");
    speech.cleanUp();
    startActivity(intent);
}

.

public class Speech implements TextToSpeech.OnInitListener {

    private String toRead;
    private TextToSpeech tts;
    public Speech(Context c) {
        tts = new TextToSpeech(c, this);
        tts.setPitch(1.2f);
        tts.setSpeechRate(1.5f);
    }
    public void speaks()
    {
        speakOut();
    }

    public void changeText(String changeTo)
    {
        toRead = changeTo;
    }


    public void cleanUp() {
         //Don't forget to shutdown tts!
        if (tts != null) {
            tts.stop();
            tts.shutdown();
        }
    }   


    public void onInit(int status) {

        if (status == TextToSpeech.SUCCESS) {

            int result = tts.setLanguage(Locale.UK);

            if (result == TextToSpeech.LANG_MISSING_DATA
                    || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e("TTS", "This Language is not supported");
            } else {
                speakOut();
            }

        } else {
            Log.e("TTS", "Initilization Failed!");
        }

    }
    protected void onStop()
    {

        if(tts != null){
            tts.shutdown();
        }       
    }
    private void speakOut() {
        tts.speak(toRead, TextToSpeech.QUEUE_FLUSH, null);
        }
    }

.

03-21 16:42:32.515: I/TextToSpeech(24023): Sucessfully bound to com.google.android.tts
03-21 16:42:32.535: E/ActivityThread(24023): Activity org.BT.Text_entry has leaked ServiceConnection android.speech.tts.TextToSpeech$Connection@427fd290 that was originally bound here
03-21 16:42:32.535: E/ActivityThread(24023): android.app.ServiceConnectionLeaked: Activity org.BT.Text_entry has leaked ServiceConnection android.speech.tts.TextToSpeech$Connection@427fd290 that was originally bound here
03-21 16:42:32.535: E/ActivityThread(24023):    at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:965)
03-21 16:42:32.535: E/ActivityThread(24023):    at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:859)
03-21 16:42:32.535: E/ActivityThread(24023):    at android.app.ContextImpl.bindService(ContextImpl.java:1344)
03-21 16:42:32.535: E/ActivityThread(24023):    at android.app.ContextImpl.bindService(ContextImpl.java:1336)
03-21 16:42:32.535: E/ActivityThread(24023):    at android.content.ContextWrapper.bindService(ContextWrapper.java:401)
03-21 16:42:32.535: E/ActivityThread(24023):    at android.speech.tts.TextToSpeech.connectToEngine(TextToSpeech.java:627)
03-21 16:42:32.535: E/ActivityThread(24023):    at android.speech.tts.TextToSpeech.initTts(TextToSpeech.java:597)
03-21 16:42:32.535: E/ActivityThread(24023):    at android.speech.tts.TextToSpeech.<init>(TextToSpeech.java:553)
03-21 16:42:32.535: E/ActivityThread(24023):    at android.speech.tts.TextToSpeech.<init>(TextToSpeech.java:527)
03-21 16:42:32.535: E/ActivityThread(24023):    at android.speech.tts.TextToSpeech.<init>(TextToSpeech.java:512)
03-21 16:42:32.535: E/ActivityThread(24023):    at org.BT.Speech.<init>(Speech.java:14)
03-21 16:42:32.535: E/ActivityThread(24023):    at org.BT.Text_entry.onCreate(Text_entry.java:97)
03-21 16:42:32.535: E/ActivityThread(24023):    at android.app.Activity.performCreate(Activity.java:5184)
03-21 16:42:32.535: E/ActivityThread(24023):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
03-21 16:42:32.535: E/ActivityThread(24023):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
03-21 16:42:32.535: E/ActivityThread(24023):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
03-21 16:42:32.535: E/ActivityThread(24023):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
03-21 16:42:32.535: E/ActivityThread(24023):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
03-21 16:42:32.535: E/ActivityThread(24023):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-21 16:42:32.535: E/ActivityThread(24023):    at android.os.Looper.loop(Looper.java:137)
03-21 16:42:32.535: E/ActivityThread(24023):    at android.app.ActivityThread.main(ActivityThread.java:4898)
03-21 16:42:32.535: E/ActivityThread(24023):    at java.lang.reflect.Method.invokeNative(Native Method)
03-21 16:42:32.535: E/ActivityThread(24023):    at java.lang.reflect.Method.invoke(Method.java:511)
03-21 16:42:32.535: E/ActivityThread(24023):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
03-21 16:42:32.535: E/ActivityThread(24023):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
03-21 16:42:32.535: E/ActivityThread(24023):    at dalvik.system.NativeStart.main(Native Method)
03-21 16:42:32.670: I/TextToSpeech(24023): Sucessfully bound to com.google.android.tts
03-21 16:42:32.670: D/(24023): 3333333333333333333333333333333333333333333333333
03-21 16:42:32.670: E/SpannableStringBuilder(24023): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
03-21 16:42:32.670: E/SpannableStringBuilder(24023): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
03-21 16:42:32.775: I/TextToSpeech(24023): Connected to TTS Service
03-21 16:42:32.780: I/TextToSpeech(24023): Connected to TTS Service

我相信这可以解决您的问题,但是我无法用您的代码来解释-也许这里的其他人可以http://www.stevenmarkford.com/android-activity-has-leaked-serviceconnection-that-was-originally -在这里绑定/

I am not sure how much this will help but Please try to implement onPause() method in Activity class. 我不确定这有多大帮助,但是请尝试在Activity类中实现onPause()方法。 And unbind your service explicitly. 并显式取消绑定服务。 For ex: 例如:

@Override
        protected void onPause() {
            super.onPause();
            CalculatorUser c=new CalculatorUser(); //your Activity name object
            c.unbindService((ServiceConnection) this);
        }

Unbinding might solve your issue and "Activity 'APP' has leaked ServiceConnection" happens mostly when you move out of your Activity So you need to explicitly tell to Android OS to please unbind me from services my Activity is using as of now. 解除绑定可能会解决您的问题,并且大多数情况下,当您退出Activity时,都会发生“ Activity'APP'已泄漏ServiceConnection”,因此您需要明确告知Android OS,以将我从Activity截至目前的服务中解除绑定。 When again you will launch you App it will bind to service. 再次启动您的应用程序时,它将绑定到服务。 If its not helpful sent me code i will try to fix it for you. 如果对我没有帮助,我会尝试为您修复代码。

ACTUAL ANSWER TO THIS PROBLEM: 实际解决此问题的方法:

You need to call destroy() on your TextToSpeech object when the service is destroyed. 服务destroy()需要在TextToSpeech对象上调用destroy()

I had this problem and saw your question so I thought I would answer it for anyone else who comes here. 我遇到了这个问题,看到了您的问题,因此我想我会为任何来这里的人回答。

For me works to shutdown the TextToSpeech object. 对我来说,要关闭TextToSpeech对象。

@Override
protected void onDestroy() {
    tts.shutdown();
    super.onDestroy();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM