VictorCharlie23
VictorCharlie23

Reputation: 121

Android - how properly trigger getExtra()?

I want to putExtra() my object into an intent then send it to a resident service and let it process the object further.

I've implemented the parcelable etc. and I've done the putExtra() code. Now I ran into a problem:I don't know how to use the getExtra() smart-ly.

Most of the examples on this site are about activity to activity communication - that is, the moment you pass your object or parcelable to another activity is the same moment you launch your target activity, which means all you have to do is to put your getExtra() in the onCreate method and you will be all set.

But as I mentioned I wanted a resident service, which means that this service will be created and started perhaps long before my sending a parcelable object.

Of course I can implement a runnable with delay timer which check the intent passed to it on a regular interval but that would be too much of a hassle. Moreover I simply refuse to believe that the android API doesn't have a way to detect a putExtra() even. I mean it seems quite unlikely.

So what should I do to get my getExtra() in my data processing service called every time a putExtra() method was called? I mean java is an OOPL, it is event driven, right? There must be something I can exploit?

Upvotes: 0

Views: 55

Answers (2)

chessdork
chessdork

Reputation: 2029

It sounds like you want your Activity to communicate with a Service. This can be solved using a Bound Service. https://developer.android.com/guide/components/bound-services.html

To get this to work, you need to override onBind in the Service, which returns an IBinder. If both your activity and service are running in the same process, this is fairly straightforward. https://developer.android.com/guide/components/bound-services.html#Binder

Then, in your activity, you get a reference to the IBinder by calling bindService, and then you can make calls directly to the Service.

Upvotes: 1

Maxim Dubovoi
Maxim Dubovoi

Reputation: 53

What about RxJava? You can provide Observable into your Service and check all changes with data.

Upvotes: 0

Related Questions