1

In my app I need to request periodic location updates, I check the settings in the Activity and then I request the location in a foreground service with LocationServices.FusedLocationApi. How to pass a LocationRequest object from the activity to the service?

Edit1: the service is started from the activity and bound. But I can't understand how to pass that object.

SOLVED: I think I've solved. I have correctly implemented binding and then I've found this answer: [1]How to send an object from one Android Activity to another using Intents? I was making a noob mistake in getting Parceable in the service, which caused a NullPointer but I've solved it. So the solution I was looking for is: Use Parceable (and implement it properly ;))

Note read this Shoaib comment:

Google recommends not to pass any extra parameters to service using intent, it might not be seen on the service side because multiple components can bind to the service and onBind() won't always be called.

Community
  • 1
  • 1
filb
  • 99
  • 1
  • 8

2 Answers2

0

Maybe you should study this

You will be able to understand how to bind and than communicate with services (involving passing object also).

Extending answer regarding your edit to question:

The system calls your service's onBind() method that returns the IBinder when the client (Your Activity) binds using bindService() method. Using this IBinder instance returned from onBind(), the caller (Your Activity) can access public methods of the service. You can have one public method that will be used for sending parameters/objects to it.

You can further refer to Google Documentation Here.

Shoaib Anwar
  • 1,555
  • 12
  • 26
  • Thanks for your reply. I forgot to mention that my service is started and bound to the activity. I'll edit the original post. – filb Feb 11 '17 at 19:07
  • If you are new to android and development, I will suggest not using _EventBus_ in places where it is not as such needed since one tends to overuse it in some cases and the code can stop making sense. Creates difficulty for you in future trying to understand you code. – Shoaib Anwar Feb 11 '17 at 19:43
  • Thank you Shoaib, I think I've solved. I have correctly implemented binding and then I've found this answer: http://stackoverflow.com/questions/2139134/how-to-send-an-object-from-one-android-activity-to-another-using-intents?rq=1 I was making a noob mistake in getting Parceable in the service, which caused a NullPointer but I've solved it. So the solution I was looking for is: use Parceable (and implement it properly ;)) – filb Feb 11 '17 at 19:55
  • ok great. But just a good tip. Google recommends not to pass any extra parameters to service using intent, it might not be seen on the service side because multiple components can bind to the service and `onBind()` won't always be called. You might have solved it in this case but its problematic. [Refer to this answer also for better understanding](http://stackoverflow.com/questions/32574059/sending-data-to-a-bound-service) – Shoaib Anwar Feb 11 '17 at 20:00
0

One of the solution is EventBus library. you can broadcast one object from everywhere and subscribe to that object on any thread you want. you can register and subscribe to that event after binding with this line:

    EventBus.getDefault().register(this);

for example in onStartCommand.

and unRegister(unsubscribe) in onDestroy

    EventBus.getDefault().unregister(this);
Siavash Abdoli
  • 1,852
  • 3
  • 22
  • 38