title: TTS inside broadcastreceiver call for incoming call/sms
body: <p>I am working on call name announcer apps. But my project doesn't speak when SMS is received. It receives it successfully, but can't speak it. I think there is a problem with call of TTS from <code>BroadcastReceiver</code>. Here is my code:</p>
  
  <pre><code>public class SMSAnnouncer extends BroadcastReceiver {
  
  @Override
  public void onReceive(Context context, Intent intent) {
      Bundle b = intent.getExtras();
      SmsMessage[] mensajes = null;
      String cadena = "";
      if (b != null) {
          Object[] objetos = (Object[]) b.get("pdus");
          mensajes = new SmsMessage[objetos.length];
  
          for (int i = 0; i &lt; mensajes.length; i++) {
              mensajes[i] = SmsMessage.createFromPdu((byte[]) objetos[i]);
              cadena += "SMS de " + mensajes[i].getOriginatingAddress();
              cadena += "Compuesto por...";
              cadena += mensajes[i].getMessageBody().toString();
              cadena += "\n";
          }
          Toast.makeText(context, cadena, Toast.LENGTH_LONG).show();
          Speaker habla = new Speaker();
          habla.speak(cadena);
      }
  }
  }
  
  
  public class Speaker extends Activity {
  
  TextToSpeech ttp;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      ttp = new TextToSpeech(getApplicationContext(), new OnInitListener() {
          @Override
          public void onInit(int status) {
              if (status != TextToSpeech.ERROR) {
                  Locale loc = new Locale("spa", "ESP");
                  ttp.setSpeechRate(2);
                  ttp.setLanguage(Locale.US);
                  ttp.setSpeechRate(0.8f);
              }
          }
      });
  }
  
  void speak(String sms) {
      ttp.speak(sms, TextToSpeech.QUEUE_FLUSH, null);
  }
  }
  </code></pre>
  
  <hr>
  
  <pre><code>&lt;application
      android:icon="@drawable/icon"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" &gt;
  
      &lt;activity
          android:name="com.callandsmsblocker.MainActivity"
          android:label="@string/app_name" &gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.MAIN" /&gt;
  
              &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
          &lt;/intent-filter&gt;
      &lt;/activity&gt;
  
      &lt;receiver android:name="com.callandsmsblocker.CallAnnouncer" &gt;
          &lt;intent-filter android:priority="1000" &gt;
              &lt;action android:name="android.intent.action.PHONE_STATE" &gt;
              &lt;/action&gt;
          &lt;/intent-filter&gt;
      &lt;/receiver&gt;
  
      &lt;receiver android:name="com.callandsmsblocker.SMSAnnouncer" &gt;
          &lt;intent-filter android:priority="1000" &gt;
              &lt;action android:name="android.provider.Telephony.SMS_RECEIVED" /&gt;
          &lt;/intent-filter&gt;
      &lt;/receiver&gt;
  &lt;/application&gt;
  </code></pre>