Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your code didn't work on :</p> <pre><code>tts = new TextToSpeech(context, this); </code></pre> <p>Context on BroadcastReceiver is a "<strong>restricted context</strong>". It means you cannot start service on context in BroadcastReceiver. Because TTS is a service, so it doesn't call anyting.</p> <p>The Best Solutions is you start another intent on BroadcastReceiver with activity that call the service.</p> <pre><code>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); .... } </code></pre> <p>And then on the activity you call the TTS service with parameter from extras</p> <pre><code>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(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload