user1165687
user1165687

Reputation: 3

Issue passing data from activity to service

'Below are my two classes which I'm trying to pass data from my activity to my service. I've passed to other Activities no problem but I when look for my extras in my IntentService it always comes back null. Any ideas what I'm doing incorrectly?

public class TestActivity extends Activity {
private Button mbutton;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mbutton = (Button) this.findViewById(R.id.button1);
    mbutton.setOnClickListener(new Button.OnClickListener(){
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
             Log.d("TestActivity", "onClick: starting srvice");
             Intent intent = new Intent(TestActivity.this/*getApplicationContext()*/, MyService.class);
             /*TestActivity.this.getApplicationContext().*/
             intent.putExtra(MyService.PARAM_IN_NAME, ((EditText)findViewById(R.id.editText1)).getText());
             intent.putExtra(MyService.PARAM_IN_JOB, ((EditText)findViewById(R.id.editText2)).getText());
             intent.putExtra(MyService.PARAM_IN_BDAY, ((EditText)findViewById(R.id.editText3)).getText());
             startService(intent);
        }
    });
  }
}

This is my Service class which I'm trying to extract my data from activity.

public class MyService extends IntentService {

public static final String PARAM_IN_NAME = "name";
public static final String PARAM_IN_JOB = "job";
public static final String PARAM_IN_BDAY = "bday";

private String mJob;
private String mBday;
private String mName;

// --------------CONSTRUCTORS--------------
public MyService() {
    super("MyServiceThread");
}

public MyService(String name) {
    super(name);
}

// -------------OVERRIDE-------------
@Override
public IBinder onBind(Intent arg0) {
    return null;
}

@Override
public void onDestroy(){
    Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show();
}

@Override
protected void onHandleIntent(Intent intent) {
    Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();

    mName = intent.getStringExtra(PARAM_IN_NAME);
    mJob = intent.getStringExtra(PARAM_IN_JOB);
    mBday = intent.getStringExtra(PARAM_IN_BDAY);

    String result = "Name = " + mBday + " | Job = " + mJob + " | Bday = " + mBday; 

    Toast.makeText(this, result, Toast.LENGTH_SHORT).show();
}

}

This is also what I have for my Manifest file:

    <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".TestActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service android:name=".MyService" android:enabled="true" />

Upvotes: 0

Views: 680

Answers (3)

Vineet Shukla
Vineet Shukla

Reputation: 24031

Your code is looking fine.

Try after adding toString() when you get text from the EditText like this:

intent.putExtra(MyService.PARAM_IN_NAME, ((EditText)findViewById(R.id.editText1)).getText().toString());

Upvotes: 1

Kamalone
Kamalone

Reputation: 4115

Use bundle for sending the data to the service.

From the activity.

Bundle bundle = new Bundle();  
bundle.putCharSequence("extraData", data);
myIntent.putExtras(bundle);

Receives in service.

Bundle bundle = intent.getExtras(); data = (String) bundle.getCharSequence("extraData");

Upvotes: 6

KaSiris
KaSiris

Reputation: 407

I hand my data over like this to the receiving end

  Bundle bundle = this.getIntent().getExtras();
    String scan = bundle.getString("scan");
    ((TextView)findViewById(R.id.username)).setText(scan);

Upvotes: 0

Related Questions