Niki
Niki

Reputation: 1181

Can a bundle be passed to a service?

In my application i need to get a value from an activity to a service. The value that i need to retrieve is the one i clicked in that activity.

For eg: If i select x[i] element from Activity A, i need to retrieve the value x[i] in a Service S.

How is it possible?

Thanks,

Niki

Upvotes: 8

Views: 8067

Answers (3)

Atmaram
Atmaram

Reputation: 3785

You can override the onStartCommand(Intent intent, int flags, int startId) method in the service.

Upvotes: 0

Reno
Reno

Reputation: 33792

In the service use this:

public int onStartCommand (Intent intent, int flags, int startId)
{
     super.onStartCommand(intent, flags, startId);
     Bundle bundle = intent.getExtras();
}

Upvotes: 7

Atmaram
Atmaram

Reputation: 3785

When you create an intent , you can put data to it and the same data will be transferred along with the Intent when you start the service.

Intent intent = new Intent(context, Class) ;
intent.putExtra(key, value);

startService(intent);

In the receiving end get the intent and get extra value from it.

Bundle b = getIntent().getExtra();
b.get<ValueType>(key);

Upvotes: 2

Related Questions