i need call tts service within subclass of broadcastreceiver. when implement class oninitlistener, gave run-time error.
is there other-way implement tts within broadcastreceiver?
thank you,
sorry code:
public class textapp extends broadcastreceiver implements oninitlistener { private texttospeech tts; private string message = "hello"; @override public void onreceive(context context, intent intent) { tts = new texttospeech(context, this); message = "hello tts"; } @override public void oninit(int status) { if (status == texttospeech.success) { tts.speak(message, texttospeech.queue_flush, null); } } }
your code didn't work on :
tts = new texttospeech(context, this);
context on broadcastreceiver "restricted context". means cannot start service on context in broadcastreceiver. because tts service, doesn't call anyting.
the best solutions start intent on broadcastreceiver activity call service.
public void onreceive(context context, intent intent) { .... intent speechintent = new intent(); speechintent.setclass(context, readthemessage.class); speechintent.putextra("message", message.getmessagebody().tostring()); speechintent.addflags(intent.flag_activity_new_task | intent.flag_activity_exclude_from_recents); context.startactivity(speechintent); .... }
and on activity call tts service parameter extras
public class readthemessage extends activity implements oninitlistener,onutterancecompletedlistener { private texttospeech tts = null; private string msg = ""; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); intent startingintent = this.getintent(); msg = startingintent.getstringextra("message"); tts = new texttospeech(this,this); } @override protected void ondestroy() { super.ondestroy(); if (tts!=null) { tts.shutdown(); } } // oninitlistener impl public void oninit(int status) { tts.speak(msg, texttospeech.queue_flush, null); } // onutterancecompletedlistener impl public void onutterancecompleted(string utteranceid) { tts.shutdown(); tts = null; finish(); } }
Comments
Post a Comment