Android应用,短信/文字转语音(已泄露serviceConnection)

时间:2011-12-21 16:09:24

标签: android sms text-to-speech serviceconnection

我有这个应用程序侦听传入的消息并大声读取它们。问题是我退出时出现以下错误

  

12-21 15:45:29.949:E / ActivityThread(566):活动mo.rach.collaboration.speaker已泄露ServiceConnection   android.speech.tts.TextToSpeech$Connection@41397858那是   最初绑在这里

我基本上希望它在后台运行,这样如果应用程序关闭,消息仍然会大声朗读。在我9号回到大学之前,我真的需要弄清楚这一点,因为这是项目的参与日期。

smsReceiver.java

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class smsreceiver extends BroadcastReceiver {


    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
                int n; 
                Bundle bundle = intent.getExtras();
                Object messages[] = (Object[]) bundle.get("pdus");
                SmsMessage smsMessage[] = new SmsMessage[messages.length];
                for (n = 0; n<messages.length; n++) 
                {
                smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
                }
                // show first message
                String sms1 = smsMessage[0].getMessageBody();
                String from = smsMessage[0].getOriginatingAddress();
                Toast toast = Toast.makeText(context,"SMS Received from "+from+":" + sms1, Toast.LENGTH_LONG);
                toast.show();
                speaker.speakSMS(sms1);             
            }

    }

speaker.java

import android.speech.tts.*;
import android.app.Activity;
import android.os.Bundle;

public class speaker extends Activity {
    /** Called when the activity is first created. */
    private static TextToSpeech myTts;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        myTts = new TextToSpeech(this, ttsInitListener);

    }
    private TextToSpeech.OnInitListener ttsInitListener = new TextToSpeech.OnInitListener() {
        public void onInit(int version) {
          //myTts.speak(""+o, 0, null);
        }
      };

   public static void speakSMS(String sms)
   {
       myTts.speak(sms, 0, null);
       myTts.synthesizeToFile(sms,null, "/sdcard/myappsounds/mysms.wav");
   }
}

1 个答案:

答案 0 :(得分:1)

在您的演讲者课程中

尝试添加:

void onDestroy() {
    if(dTTS != null) {
       dTTS.stop();
       dTTS.shutdown();
    }
    super.onDestroy();
}