Android 应用程序、短信/文本转语音(已泄露服务连接)
我有这个应用程序侦听传入的消息并大声朗读它们。问题是当我退出时出现以下错误
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在你的演讲者课程中尝试添加:
in your speaker class try to add: