Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Starting a service with extra information through put/getExtra

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.

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?

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.

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):

@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;

}

My activity (based on Sai Geetha's blog):

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(); 
         }
     }                 
 };

}

And this is my service:

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();    <-- this has null extras, too...

    return null;
}

Bundle extras;

@Override
public void onCreate() {
      super.onCreate();

      // extras = getIntent().getExtras();   <-- 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();

}
}
like image 803
Adam Avatar asked Nov 13 '11 15:11

Adam


People also ask

What does intent put extra do?

PutExtra(String, String)Add extended data to the intent.

What is put extra in android?

The Android Intent class is commonly used to move from one activity to another. The Intent class has a method named putExtra() that you can use to put extended data to the intent. The main use of the putExtra() method is to send values you need in the next activity.

How do I set extras intent?

Using putExtra() We can start adding data into the Intent object, we use the method defined in the Intent class putExtra() or putExtras() to store certain data as a key value pair or Bundle data object. These key-value pairs are known as Extras in the sense we are talking about Intents.


1 Answers

you need to look at the onStartCommand() function (I'm not at a desktop so I can't conveniently link to the javadoc). This will receive the intent you passed and your extras should be available there.

like image 113
Femi Avatar answered Oct 06 '22 19:10

Femi