title: Android- Receiver not registered
body: <p>I'm trying to write a simple app that will use textToSpeech to read income SMS, using broadcastReceiver.</p>
  
  <p>This is the Activity:</p>
  
  <pre><code>public class MainActivitySmsToVoice extends Activity {
  
  private TextToSpeech engine;
  private static final int STATUS_CHECK = 0;
  Button btnStart;
  Button btnStop;
  MySmsToVoiceReceiver br;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main_activity_sms_to_voice);
  
      btnStart = (Button) findViewById(R.id.button1);
      btnStop = (Button) findViewById(R.id.button2);
  
      btnStart.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View v) {
              btnStart.setVisibility(View.VISIBLE);
              btnStop.setVisibility(View.VISIBLE);
  
              br = new MySmsToVoiceReceiver();
              //register SMS receiver
              registerReceiver(br, new IntentFilter("android.provider.Telephony.SMS_RECEIVED"));
          }
      });
      Intent checkIntent = new Intent();
      checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
      startActivityForResult(checkIntent, STATUS_CHECK);
  }
  
  //This method will be called after startActivityForResult() finishes, 
  //and check weather the text to speech installed on device
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  
      switch (resultCode) {
      case TextToSpeech.Engine.CHECK_VOICE_DATA_PASS:
          engine = new TextToSpeech(this, (OnInitListener) this);
          break;
      case TextToSpeech.Engine.CHECK_VOICE_DATA_FAIL:
          Intent installIntent = new Intent();
          installIntent
                  .setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
          startActivity(installIntent);
          break;
      default:
          Log.e("textToSpeach", "Unable to intall TextToSpeach module");
      }
  }
  
  @Override
  protected void onPause() {
      super.onPause();
      unregisterReceiver(br);
      btnStop.setVisibility(View.INVISIBLE);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>This is the BroadcastReceiver:</p>
  
  <pre><code>public class MySmsToVoiceReceiver extends BroadcastReceiver {
  private static final String SMS_RECEIVE_ACTION = "android.provider.Telephony.SMS_RECEIVED";
  private TextToSpeech engine;
  
  @Override
  public void onReceive(Context context, Intent intent)
  {
      Log.i("sms_receiver","within the onReceive method");
      if (intent != null
              &amp;&amp; intent.getAction() != null
              &amp;&amp; intent.getAction().compareToIgnoreCase(SMS_RECEIVE_ACTION) == 0)
      {
          Object[] vec = (Object[]) intent.getExtras().get("pdus");
          SmsMessage[] messages = new SmsMessage[vec.length];
          for (int i = 0; i &lt; vec.length; i++)
          {
              messages[i] = SmsMessage.createFromPdu((byte[]) vec[i]);
              Log.i("sms_receiver", messages[i].getMessageBody());
              //speak the message
              engine.speak( messages[i].getMessageBody().toString(), 
                      TextToSpeech.QUEUE_ADD, null);
          }
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>When running i get error:</p>
  
  <blockquote>
    <p>java.lang.RuntimeException: Unable to pause activity
    java.lang.IllegalArgumentException: Receiver not registered: null</p>
  </blockquote>
  
  <p>What am i doing wrong?</p>