gpt4 book ai didi

Android 应用程序,SMS/文本到语音(已泄漏 serviceConnection)

转载 作者:行者123 更新时间:2023-11-30 04:25:39 26 4
gpt4 key购买 nike

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

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

我基本上希望它在后台运行,这样即使应用程序关闭,消息仍会被大声朗读。我真的需要在 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");
}
}

最佳答案

在你的演讲者类(class)中尝试添加:

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

关于Android 应用程序,SMS/文本到语音(已泄漏 serviceConnection),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8592820/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com