我有这个应用程序监听传入的消息并大声朗读它们。问题是我退出时出现以下错误
12-21 15:45:29.949: E/ActivityThread(566): Activity mo.rach.collaboration.speaker 泄露了原本绑定在这里的 ServiceConnection android.speech.tts.TextToSpeech$Connection@41397858
我基本上希望它在后台运行,这样如果应用程序关闭,消息仍然会被大声朗读。我真的需要在 9 号回到大学之前弄清楚这一点,因为这是该项目的交接日期。
短信接收器.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);
}
}
扬声器.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");
}
}