0

I have a service that is in a different java file than the file that is starting the service.

Let's call one Main.java and one ServiceFile.java for simplicity.

Main starts the service(ServiceFile.java) in onCreate() and the service continues running for the life of the application. The function of the service is to perform network operations on a separate thread and then after receiving data, creates a Vehicle object.

I need to get this object from the ServiceFile.java to Main.java

I have read about Handlers on the google devs and it seems like what I need. However, all examples that I can find of implementation of Handlers are ones where other threads are in the same java file. How do I sendMessage across files like this?

I had the idea of using putExtra to give a reference to the Handler but that wouldn't work because Handler isn't parcelable or serializable.

benzabill
  • 259
  • 1
  • 8
  • 21

2 Answers2

2

I think a better way would be if you can bind to the service from your Second Activity and then use the Binder to get the object from the Service. Take a look at this answer here

Basically, what is being done in that answer is, you get a IBinder from the Service and hence a handle to the Service itself(which is already running). Now that you have a handle to the service, you can get the VO directly from the service.

Community
  • 1
  • 1
Varun
  • 33,833
  • 4
  • 49
  • 42
  • Well the problem is I don't want to bind the service, I wanted to just have the service constantly running. An idea I just had was call a (static?)method in Main.java that is something like, getVehicle(Vehicle v) and then do stuff with that. – benzabill Sep 06 '13 at 00:35
  • Binding to a `Service` doesnt mean it will not be constantly running. YOu start the service the way you usually start and then bind to it and do things as and when needed. How do you think the music app does this? The services is running constantly and the Activity binds to it whenever needed. For instance when you tap on the play/pause button, the activity binds to service does stuff. – Varun Sep 06 '13 at 08:37
  • To be clear enough, I am not talking about `Bound Services`. Those kind of services do not run in the background indefinitely. What Im talking about is binding to an already running Service. Hope you see the difference. – Varun Sep 06 '13 at 08:56
  • So as I understand from the link that you sent, I can make a new Messenger(myHandler) and then putExtra("Key" , myHandler). Since I can do this, I can pass a reference to the handler from my Main.java to ServiceFile.java through the intent that I use to start the service. Yes? – benzabill Sep 06 '13 at 14:58
  • Ignore the `handler-messenger` thing there. Look at `onServiceConnected` in the answer. You will see that you can get a handle to the Service itself and then call a public method to get data from the service. – Varun Sep 06 '13 at 17:54
0

There is a complete guide to implementing the type of task that you need on the Android developers page:

Link: Bound Service

Diego Palomar
  • 6,958
  • 2
  • 31
  • 42