umair umair - 1 year ago 148
Android Question

Start service after setOnUtteranceProgressListener on done?

I have a problem i want to start android service after Text to speech finish speak.

Here is my Code

HashMap<String, String> myHashAlarm = new HashMap<String, String>();
myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_ALARM));
myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "SOME MESSAGE");
t1.speak(text, TextToSpeech.QUEUE_FLUSH, myHashAlarm);
t1.speak("I can speak anything",TextToSpeech.QUEUE_ADD, null);

public void onInit(int status) {
if (status != TextToSpeech.ERROR) {
t1.setOnUtteranceProgressListener(new UtteranceProgressListener() {
public void onDone(String utteranceId) {
// Log.d("MainActivity", "TTS finished");
i want to start my service here.......


public void onError(String utteranceId) {

public void onStart(String utteranceId) {

Please help me....

Answer Source

You can start the service in the standard way, but startService() requires context and if you are inside your ProgressListener, that will be the context.

You'll need to use

context.startService(new Intent(context, MyService.class));

If you are using from an Activity, you can use

MyActivity.this.startService(new Intent(MyActivity.this, MyService.class));

There are more examples on this question. You can of course create an intent and add extras to it, before using it in this way.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download