title: How to use Intents to pass data from Activity to BroadcastReceiver
body: <p>I have just started my first main project - a simple anti-theft application, that when my the phone is been text-ed, if it contains a certain string, it will send a sms back to the originating address, telling it the location of the phone. However, I am at the moment only trying to get the to phone receive a sms, check if it is containing the string, get the location, and then toast the location on the phone. My problem is, as I am very very new to android programming/Java (Only 15) I'm having trouble with passing my location from my MainActivity to my Broadcast Receiver. </p>
  
  <p>This is my MainActivity - where my LocationListener is, as you can see, I have commented out the Toast.makeText() on the onLocationChanged, if I un-comment it, I do get the correct co-ordinates. </p>
  
  <pre><code>package com.SMS;
  
  import android.app.Activity;
  import android.content.Context;
  import android.content.Intent;
  import android.os.Bundle;
  import android.location.Location;
  import android.location.LocationListener;
  import android.location.LocationManager;
  
  public class MainActivity extends Activity 
  {
  
      private LocationManager lm;
      private LocationListener locationListener;
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState)
      {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          lm = (LocationManager)
              getSystemService(Context.LOCATION_SERVICE);
  
          locationListener = new MyLocationListener();
  
          lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);    
      }
  
   public class MyLocationListener implements LocationListener
   {
      @Override
      public void onLocationChanged(Location loc) {
              if (loc != null) 
              {
                  int Longitude = (int) (loc.getLongitude() * 1E6);
                  int Latitude = (int) (loc.getLatitude() * 1E6);
                  // Toast.makeText(getBaseContext(), "Location Changed : Lat: " + Latitude + " Longitude : " + Longitude, Toast.LENGTH_LONG).show();                 sendBroadcast(intent);
  
                  Intent intent = new Intent(MainActivity.this, SMSReceiver.class); 
                  intent.putExtra("Longitude", Longitude);
                  intent.putExtra("Latitude", Latitude);
                  sendBroadcast(intent);
              }
      }
  
      @Override
      public void onProviderDisabled(String provider) {
          // TODO Auto-generated method stub
  
      }
  
      @Override
      public void onProviderEnabled(String provider) {
          // TODO Auto-generated method stub
  
      }
  
      @Override
      public void onStatusChanged(String provider, int status, Bundle extras) {
          // TODO Auto-generated method stub
  
      }
    }
  }
  </code></pre>
  
  <p>This is my SmsReceiver class, this is where the sms is retrieved to check for the containing string. I have tried everything that I have seen on this sort of thing (My history is full of this stuff), but what ever I did, the latitude and longitude still return 300 - the default value which I set. I am sure it is something to do with my intent setup and receiving the intent, just don't know what it is what is wrong. So, please read through and give me feedback on anything you find wrong with it. </p>
  
  <pre><code>package com.SMS;
  
  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  import android.os.Bundle;
  import android.telephony.SmsMessage;
  import android.widget.Toast;
  
  public class SMSReceiver extends BroadcastReceiver
  {
  
      @Override
      public void onReceive(Context context, Intent intent) 
      {
  
          //--get the sms message passed in--
          Bundle bundle = intent.getExtras();
          SmsMessage[] msgs = null;
          String str = "";
          if (bundle != null) 
          {
              //--- retrieve the sms message received
              Object[] pdus = (Object[]) bundle.get("pdus");
              msgs = new SmsMessage[pdus.length];
              for (int i=0; i&lt;msgs.length; i++)
              {
                  msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
  
                  str += "SMS from " + msgs[i].getOriginatingAddress();
                  str += " : ";
                  str += msgs[i].getMessageBody().toString();
                  str += "\n";
                  if(str.contains("hi"))
                  {
                      int longitude = intent.getIntExtra("Longitude", 300);
                      int latitude = intent.getIntExtra("Latitude", 300);
                      //--display the new sms message
                      Toast.makeText(context, "Location Changed : Lat: " + latitude + " Longitude : " + longitude, Toast.LENGTH_LONG).show();
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>And finally, my Manifest, for some reason I have a inckling that this may be part of my problem, but I don't really know why - if you see anything wrong or out of place, or can be simplified, please tell me </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.SMS"
      android:versionCode="1"
      android:versionName="1.0" &gt;
  
      &lt;uses-sdk
          android:minSdkVersion="8"
          android:targetSdkVersion="17" /&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" &gt;
          &lt;activity
              android:name="com.SMS.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=".SMSReceiver" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.provider.Telephony.SMS_RECEIVED" &gt;
                      &lt;action android:name=".com.SMS.Broadcast" &gt;
                      &lt;/action&gt;
                  &lt;/action&gt;
             &lt;/intent-filter&gt;
          &lt;/receiver&gt;
      &lt;/application&gt;
  
      &lt;uses-permission android:name="android.permission.SEND_SMS" &gt;
      &lt;/uses-permission&gt;
      &lt;uses-permission android:name="android.permission.RECEIVE_SMS" &gt;
      &lt;/uses-permission&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" &gt;
      &lt;/uses-permission&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_COURSE_LOCATION" &gt;
      &lt;/uses-permission&gt;
      &lt;uses-permission android:name="android.permission.INTERNET" &gt;
      &lt;/uses-permission&gt;
  &lt;/manifest&gt;
  </code></pre>
  
  <p>So, if you can give me feedback on what is wrong and how to change it, or have any questions about it, please respond because I have been spending hours just searching the web. Also, if you see anything that is 'bad coding', please don't hesitate to tell me right. 
  Thanks You so much if you can fix the problem!</p>