Android 应用程序、短信/文本转语音(已泄露服务连接)

发布于 2024-12-22 02:59:24 字数 2399 浏览 1 评论 0原文

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

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");
   }
}

I have this application listens for incoming messages and reads them aloud. The problem is I get the following error when I exit

12-21 15:45:29.949: E/ActivityThread(566): Activity mo.rach.collaboration.speaker has leaked ServiceConnection
android.speech.tts.TextToSpeech$Connection@41397858 that was
originally bound here

I basically would like it to run in the background so that if the app is closed that messages are still read aloud. I really need to figure this out before I go back to uni on the 9th as this is the hand-in date for the project.

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");
   }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执手闯天涯 2024-12-29 02:59:24

在你的演讲者课程中尝试添加:

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

in your speaker class try to add:

void onDestroy() {
    if(dTTS != null) {
       dTTS.stop();
       dTTS.shutdown();
    }
    super.onDestroy();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文