title: Android - Starting a service with extra information through put/getExtra
body: <p>I've really tried to get through the intent.putExtra() and getIntent().getExtras() and apply them to one of the SimpleService tutorials.  I know a lot of people have already asked "why is bundle extras always null?"  I promise I tried to hack through the answers I found here for several hours before I considered posting but I don't think I'm advanced enough to really understand what it is I must be doing wrong with the minor snippets people are posting.  As such I put in the full code of my activity and my service.</p>
  
  <p>I think my issue is the that my starting intent (the one I create in my activity) doesn't exist in the context of my service.  I wonder if maybe I'm using Intents in the wrong direction/purpose entirely?  I did try an intent.putExtra in my service, to try to send a string the other direction, but those extras are always null, too.  So at the risk of repetition, why is bundle extras always null?  How do I make a single intent that exists both in the context of my activity and my service?  </p>
  
  <p>I should note that the code as displayed below obviously will have a null extras  because I've commented out a few of my attempts to .getExtras() that have failed.  I deleted the rest for the sake of cleanliness.</p>
  
  <p>EDIT: The answer thanks to the replies, in code for the sake of those who have also been Googling for hours.  Put this in your service (please note that the return START_REDELIVER_INTENT may be wrong):</p>
  
  <pre><code>@Override
  public int onStartCommand( Intent intent , int flags , int startId )
  {
        super.onStartCommand(intent, flags , startId);
  
        extras = intent.getExtras();
  
            //just checking
        if( extras != null )
        Toast.makeText(this,extras.getString("extratoservice"), Toast.LENGTH_SHORT).show();
  
        return START_REDELIVER_INTENT;
  
  }
  </code></pre>
  
  <p>My activity (based on Sai Geetha's blog):</p>
  
  <pre><code>package com.example.BroadcastIntent;
  
  import android.app.Activity;
  import android.content.Intent;
  import android.os.Bundle;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.Toast;
  
  public class BroadcastIntentActivity extends Activity {
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
  
      Button start = (Button)findViewById(R.id.buttonStart);
      start.setOnClickListener(startListener);
  
      Button stop = (Button)findViewById(R.id.buttonStop);
      stop.setOnClickListener(stopListener);   
  
      //the intent I'm using to start and stop the service -- the extras don't go anywhere....
      intent = new Intent(BroadcastIntentActivity.this,BroadcastService.class);     
      intent.putExtra("extratoservice", "if you can read this, it made it to the service" );
  
  }
  
  Boolean serviceRunning;
  Intent intent;
  
  //Clicks from Geetha's Blog
  private OnClickListener startListener = new OnClickListener() {
       public void onClick(View v){
  
           startService(intent);
           serviceRunning = true;
       }                 
  };
  
  private OnClickListener stopListener = new OnClickListener() {
      public void onClick(View v){
  
           try
           {
               stopService(intent);
               serviceRunning = false;                
           }
           catch( Exception e)
           {
               Toast.makeText(getApplicationContext(), "Service was not running...",Toast.LENGTH_SHORT).show(); 
           }
       }                 
   };
  
  }
  </code></pre>
  
  <p>And this is my service:</p>
  
  <pre><code>package com.example.BroadcastIntent;
  
  import android.app.Service;
  import android.content.Intent;
  import android.os.Bundle;
  import android.os.IBinder;
  import android.widget.Toast;
  
  public class BroadcastService extends Service{
  
  @Override
  public IBinder onBind(Intent arg0) {
      // TODO Auto-generated method stub
  
      //extras = arg0.getExtras();    &lt;-- this has null extras, too...
  
      return null;
  }
  
  Bundle extras;
  
  @Override
  public void onCreate() {
        super.onCreate();
  
        // extras = getIntent().getExtras();   &lt;-- this is undefined?
  
        if( extras == null )
        Toast.makeText(this,"Service created... extras still null", Toast.LENGTH_SHORT).show();
        else
        Toast.makeText(this,extras.getString("extratoservice"), Toast.LENGTH_SHORT).show();
  
  }
  
  @Override
  public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "Service destroyed ...", Toast.LENGTH_SHORT).show();
  
  }
  }
  </code></pre>