umair umair - 4 months ago 11
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);





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

}

@Override
public void onError(String utteranceId) {
}

@Override
public void onStart(String utteranceId) {
}
});;
}
}


Please help me....

Answer

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.