title: How to Pass Data From a BroadcastReceiver to an Activity in Android
body: <p>I am working on my first Android app, which is a paging device. It will intercept a SMS message from a certain number with a certain content in it, display that and then allow the user to send a pre-defined reply back to that same number. I have gathered up code snippets from numerous sources (including stackoverflow of course) but I haven't yet got it working.</p>
  <p>My file structure is as shown here</p>
  <p><a href="https://i.stack.imgur.com/QhnyE.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QhnyE.jpg" alt="enter image description here" /></a></p>
  <p>The part I am struggling with is SmsBroadcastReceiver and ReceiveAlert, which should display the content of the SMS and has a button to initiate the reply.</p>
  <p>SmsBroadcastReceiver.java looks like this:</p>
  <pre><code>package com.example.alert6;
  
  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  import android.provider.Telephony;
  import android.telephony.SmsMessage;
  
  public class SmsBroadcastReceiver extends BroadcastReceiver {
      public static final String EXTRA_MESSAGE = &quot;com.example.alert6.MESSAGE&quot;;
  
      @Override
      public void onReceive(Context context, Intent intent) {
              String smsSender = &quot;&quot;;
              String smsBody = &quot;&quot;;
              for (SmsMessage smsMessage : Telephony.Sms.Intents.getMessagesFromIntent(intent)) {
                  smsSender = smsMessage.getOriginatingAddress();
                  smsBody = smsMessage.getMessageBody();
              }
  
              if (smsSender.equals(&quot;+420775367297&quot;)) {
                  if (smsBody.contains(&quot;Test&quot;)) {
                      intent.putExtra(EXTRA_MESSAGE, smsBody);
                      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // adding this flag starts the new Activity in a new Task
                      context.startActivity();
                  }
              }
      }
  
  }
  </code></pre>
  <p>ReceiveAlertActivity.java is this:</p>
  <pre><code>package com.example.alert6;
  
  import androidx.appcompat.app.AppCompatActivity;
  import androidx.core.app.ActivityCompat;
  import androidx.core.content.ContextCompat;
  
  import android.Manifest;
  import android.content.Intent;
  import android.content.pm.PackageManager;
  import android.os.Bundle;
  
  import android.view.View;
  import android.widget.TextView;
  
  public class ReceiveAlertActivity extends AppCompatActivity {
  
      private static final int SMS_PERMISSION_CODE = 101;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_receive_alert);
      }
  
      // Get the Intent that started this activity and extract the string
      Intent intent = getIntent();
      String taskingalert = intent.getStringExtra(SmsBroadcastReceiver.EXTRA_MESSAGE);
  
      // Capture the layout's TextView and set the string as its text
      TextView receivedAlert = findViewById(R.id.receivedAlert);
          receivedAlert.setText(taskingalert);
  
  public boolean respond(View view) {
          if (!hasReadSmsPermission()) {
              requestReadAndSendSmsPermission();
              return false;
          }
          Intent intent = new Intent(this, SendResponseActivity.class);
          startActivity(intent);
          return false;
      }
  
      /**
       * Runtime permission shenanigans
       */
      private boolean hasReadSmsPermission() {
          return (ContextCompat.checkSelfPermission(ReceiveAlertActivity.this,
                  Manifest.permission.READ_SMS) == PackageManager.PERMISSION_GRANTED) &amp;&amp;
                  (ContextCompat.checkSelfPermission(ReceiveAlertActivity.this,
                          Manifest.permission.RECEIVE_SMS) == PackageManager.PERMISSION_GRANTED) &amp;&amp;
                  (ContextCompat.checkSelfPermission(ReceiveAlertActivity.this,
                          Manifest.permission.SEND_SMS) == PackageManager.PERMISSION_GRANTED);
      }
  
      private void requestReadAndSendSmsPermission() {
          ActivityCompat.requestPermissions(ReceiveAlertActivity.this, new String[]{Manifest.permission.READ_SMS, Manifest.permission.RECEIVE_SMS, Manifest.permission.SEND_SMS},
                  SMS_PERMISSION_CODE);
      }
  }
  </code></pre>
  <p>And the manifest is this:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      package=&quot;com.example.alert6&quot;&gt;
  
      &lt;uses-permission android:name=&quot;android.permission.RECEIVE_SMS&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.READ_SMS&quot; /&gt;
      &lt;uses-permission android:name=&quot;android.permission.SEND_SMS&quot; /&gt;
  
      &lt;application
          android:allowBackup=&quot;true&quot;
          android:icon=&quot;@mipmap/ic_launcher&quot;
          android:label=&quot;@string/app_name&quot;
          android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
          android:supportsRtl=&quot;true&quot;
          android:theme=&quot;@style/Theme.Alert6&quot;&gt;
  
          &lt;activity
              android:name=&quot;.SendResponseActivity&quot;
              android:parentActivityName=&quot;.ReceiveAlertActivity&quot;&gt;
          &lt;/activity&gt;
  
          &lt;activity
              android:name=&quot;.ReceiveAlertActivity&quot;
              android:parentActivityName=&quot;.MainActivity&quot;&gt;
          &lt;/activity&gt;
  
          &lt;activity android:name=&quot;.MainActivity&quot;&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
                  &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
  
          &lt;receiver
              android:name=&quot;.SmsBroadcastReceiver&quot;
              android:enabled=&quot;true&quot;
              android:exported=&quot;true&quot;
              tools:ignore=&quot;Instantiatable&quot;&gt;
              &lt;intent-filter android:priority=&quot;999&quot; &gt;
                  &lt;action android:name=&quot;android.provider.Telephony.SMS_RECEIVED&quot; /&gt;
              &lt;/intent-filter&gt;
          &lt;/receiver&gt;
  
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  <p>Android Studio is showing errors in SmsBroadcastReceiver and ReceiveAlertActivity but not giving enough information to resolve them.</p>
  <p>In SmsBroadcastReceiver, it tells me it cannot resolve method 'startActivity()'. Something needs to go in the brackets, but what?</p>
  <p>In ReceiveAlertActivity the problems revolve around receivedalert and taskingalert. It cannot resolve setText because taskingalert is an unknown class. Obviously it's not a class, it's a string so I'm doing something wrong, but what?</p>
  <p>Sorting out these problems may not be the end. At the moment I can't test if the app works because the build fails due to the above. Then if I get this lot working, I have some other challenges, like waking up the screen and playing a sound the broadcast receiver is triggered, and stopping the sound when the button is pressed.</p>