title: android - Passing message from broadcast to activity
body: <p>I was reading some post but none have a good answer for me.</p>
  
  <p>So, which is the best way to pass data from broadcast to activity without restart the activity?</p>
  
  <p>Actually I'm using this.</p>
  
  <p>SMSListener:</p>
  
  <pre><code>public class SmsListener extends BroadcastReceiver  {
  private OnSmsReceivedListener listener = null;
  @Override
  public void onReceive(Context context, Intent intent) {
  
      try {
          if (Telephony.Sms.Intents.SMS_RECEIVED_ACTION.equals(intent.getAction())) {
              if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.KITKAT) {
                  for (SmsMessage smsMessage : Telephony.Sms.Intents.getMessagesFromIntent(intent)) {
                      String messageBody = smsMessage.getMessageBody();
                      String phoneNumber = smsMessage.getDisplayOriginatingAddress();
                      if (listener != null) {
                          listener.onSmsReceived(phoneNumber, messageBody);
                      } 
                  }
              }
          }
      } catch (Exception e)
      {
          Log.e("Error", "Some some");
      }
  }
  
  public void setOnSmsReceivedListener(Context context) {
      this.listener = (OnSmsReceivedListener) context;    
  }
  }
  </code></pre>
  
  <p>OnSmsReceivedListener:</p>
  
  <pre><code>public interface OnSmsReceivedListener {
  void onSmsReceived(String sender, String message);
  }
  </code></pre>
  
  <p>Activity:</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity implements OnSmsReceivedListener {
     private SmsListener receiver;
  
      @Override
  
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.test);
  
          /***********/
             receiver = new SmsListener();
          receiver.setOnSmsReceivedListener(this);
      }
  
      @Override
      public void onSmsReceived(String sender, String message) {
          Log.e("Test", "Sender: "+sender+" - Message: "+message);
      }
  
  }
  </code></pre>
  
  <p>Another of my questions is why I never get log "Test" in my activity. Is like listener is always null, why?</p>