title: Communication between BroadcastReceiver and Activity using interface
body: <p>Folks!
  I'm trying to implement <code>BroadcastReceiver</code> for listening to incoming Message using <code>SMSRetrieverAPI</code>. </p>
  
  <p>I'm getting messages in Broadcast's <code>onReceive</code> method but after that, I have to pass that Message String in activity without restarting it, So I have implemented interface.</p>
  
  <p>I have tried below code but, it throws <code>NullPointerException</code> for <code>mOTPReceiveListener</code>. Something didn't work perfectly so please correct me.</p>
  
  <p><code>SMSBroadcastReceiver</code> is like below</p>
  
  <pre><code>public class SMSBroadcastReceiver extends BroadcastReceiver {
  
  OTPReceiveListener mOTPReceiveListener = null;
  
  public void InitOTPReceiveListener(OTPReceiveListener otpreceivelistener){
  
      this.mOTPReceiveListener = otpreceivelistener;
  }
  
  @Override
  public void onReceive(Context context, Intent intent) {
  
      if (SmsRetriever.SMS_RETRIEVED_ACTION.equals(intent.getAction())) {
          Bundle extras = intent.getExtras();
          Status status = (Status) extras.get(SmsRetriever.EXTRA_STATUS);
  
          switch(status.getStatusCode()) {
              case CommonStatusCodes.SUCCESS:
                  // Get SMS message contents
                  String message = (String) extras.get(SmsRetriever.EXTRA_SMS_MESSAGE);
                  Log.e("VERIFY","Full Message : "+message);
  
                  if (mOTPReceiveListener != null) {
                      String[] temp = message.split("-");
                      String[] msg = temp[1].split(" ");
                      message = msg[0];
  
                      Log.e("VERIFY","message : "+message);
                      mOTPReceiveListener.onOTPReceived(message);
  
                  }
  
                  break;
              case CommonStatusCodes.TIMEOUT:
                  // Waiting for SMS timed out (5 minutes)
                  mOTPReceiveListener.onOTPTimeOut();
                  break;
          }
      }
  }
  
  public interface OTPReceiveListener {
      void onOTPReceived(String otp);
  
      void onOTPTimeOut();
  }
  </code></pre>
  
  <p>}</p>
  
  <p>and the activity code </p>
  
  <pre><code>public class VerificationActivity extends BaseActivity implements SMSBroadcastReceiver.OTPReceiveListener
  {
      Context mContext;
      Button btn_verify_submit;
      TextView text_resend_otp;
  
      private String TAG = "VERIFY";
  
      private String PHONE = null;
  
      private SmsRetrieverClient mSmsRetrieverClient;
      private SMSBroadcastReceiver mSMSBroadcastReceiver;
      SMSBroadcastReceiver.OTPReceiveListener mOTPReceiveListener;
  
  
      @Override
      protected void onCreate(@Nullable Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          setContentView(R.layout.activity_verification);
  
          initView();
          setClickListener();
  
          PHONE = getIntent().getStringExtra("phone");
          Log.e(TAG,"phone : "+PHONE);
  
          mSmsRetrieverClient = SmsRetriever.getClient(this);
          startVerify();
  
          mOTPReceiveListener = this;
          mSMSBroadcastReceiver = new SMSBroadcastReceiver();
          mSMSBroadcastReceiver.InitOTPReceiveListener(this);
  
      }
  
      private void initView()
      {
          layout_verification_main = findViewById(R.id.layout_verification_main);
          overrideFonts(layout_verification_main,VerificationActivity.this);
  
          edt_verify_code = findViewById(R.id.edt_verify_code);
  
          btn_verify_submit = findViewById(R.id.btn_verify_submit);
  
          text_resend_otp = findViewById(R.id.text_resend_otp);
      }
  
      private void setClickListener()
      {
          btn_verify_submit.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
  
                  //Call OTP Match API
  
              }
          });
  
          text_resend_otp.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  //Call Resend OTP API 
              }
          });
      }
  
  
  
  
      private void startVerify() {
          Task&lt;Void&gt; task = mSmsRetrieverClient.startSmsRetriever();
          task.addOnSuccessListener(new OnSuccessListener&lt;Void&gt;() {
              @Override
              public void onSuccess(Void aVoid) {
  
                  Log.e(TAG, "SmsRetrievalResult start onSuccess.");
  
  
              }
          });
  
          task.addOnFailureListener(new OnFailureListener() {
              @Override
              public void onFailure(@NonNull Exception e) {
                  Log.e(TAG, "SmsRetrievalResult start failed.", e);
              }
          });
  
      }
  
      @Override
      public void onOTPReceived(String otp) {
          edt_verify_code.setText("" + otp);
          Toast.makeText(mContext, "Message OTP :  " + otp, Toast.LENGTH_SHORT).show();
          Log.e("VERIFY","otp in activity : "+otp);
      }
  
      @Override
      public void onOTPTimeOut() {
  
      }
  }
  </code></pre>
  
  <p>and I have registered in Manifest like this</p>
  
  <pre><code>&lt;receiver android:name=".broadcast.SMSBroadcastReceiver" android:exported="true"&gt;
      &lt;intent-filter&gt;
          &lt;action android:name="com.google.android.gms.auth.api.phone.SMS_RETRIEVED"/&gt;
      &lt;/intent-filter&gt;
  &lt;/receiver&gt;
  </code></pre>
  
  <p>Am I missing something?</p>