Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8987031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:41:36+00:00 2026-06-15T21:41:36+00:00

So I have a bit of a problem. I am trying to make my

  • 0

So I have a bit of a problem. I am trying to make my app do things based on the message it receives through GCM. In this case it’s supposed to make a sound by using the TextToSpeech class. It kind of works, but not the first time I send the message. I realise this is probably because TextToSpeech hasn’t been instantiated, but I’m not sure how go to about and do that? I tried onInit(), but that didn’t work at all.

Also, what is the best way to shut down TTS in my example?

Disclaimer: I come from a PHP background, and know very little Java. I try to learn by doing, so please forgive me if this is a silly question. Thanks in advance!

public class GCMIntentService extends GCMBaseIntentService {

private static final String TAG = "GCMIntentService";
public static TextToSpeech mtts;

public GCMIntentService() {
    super(SENDER_ID);
}

@Override
protected void onMessage(Context context, Intent intent) {
    Log.i(TAG, "Received message");
    String message = intent.getExtras().getString("message");
    mtts = new TextToSpeech(context, null);

    if (message.startsWith("makeSound")) {
        mtts = new TextToSpeech(context, null);
        mtts.setLanguage(Locale.US);
        mtts.speak(message, TextToSpeech.QUEUE_FLUSH, null);
    }
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T21:41:37+00:00Added an answer on June 15, 2026 at 9:41 pm

    It doesn’t work the first time because the initialization of TextToSpeech is asynchronous. You can not simple instantiate it and use it as you are doing. You should provide a callback to be called once the TextToSpeech has been initialized if you want to use it right away.

     mTextToSpeech = new TextToSpeech( this, new TextToSpeech.OnInitListener()
        {
            @Override
            public void onInit( int status )
            {
                // Check for status might not be initialized due to errors
                // Configure language/speed
            }
        } );
    

    It does work the rest of the times, because mtts is static. This means that is a class variable and is not destroyed/initialized when creating new instances of the service. By the second time you use this service, this variable was already initialized in the first service execution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bit of a strange problem. I am trying to send in-app
I have a bit of problem when trying to validate my page as HTML5.
I have a bit of a problem. I am trying to do the following
So, I have a bit of a problem. I'm trying to convert an array
I have a bit of a strange problem. I have a music app that
I'm trying to make photo album like photo.app in iPhone. I know We have
I am trying to make a simple app for myself and play a bit
I am having a bit of a problem trying to get this code to
i have a bit of a dilemma, i am trying to make it so
I have a bit of a problem that I can't seem to code my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.