2

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");
//here i want to start my service
startService(new Intent(this, MyService.class));// but its not working

                        }

                        @Override
                        public void onError(String utteranceId) {
                        }

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

Please help me....

Sami
  • 8,168
  • 9
  • 66
  • 99
umair
  • 607
  • 4
  • 18
  • I'm not sure I understand the problem - why can't you just start the service where you want to using the standard method? http://stackoverflow.com/a/12555333/1256219 – brandall Jul 31 '16 at 12:15
  • startService(new Intent(this, MyService.class)); Cannot resolve constructor 'Intent(anonymous android.speech.tts.utterenceProgressListener,java.lang.Class' – umair Jul 31 '16 at 12:21
  • You need to use the correct context. See my answer and the link to the documentation. – brandall Jul 31 '16 at 12:38

1 Answers1

1

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.

Community
  • 1
  • 1
brandall
  • 6,094
  • 4
  • 49
  • 103