views:

62

answers:

3

I have a service running on my android application, the service is a socket class that has to run through the whole application, i create it when my first activity loads. How can i get an instance of this socket class so that i can call a function in this class.

For example
//how can i get an instance of the running service?
MyService s = getService();
s.writeDataToServer("hello");
+1  A: 

The Android Interface Definition Language (AIDL) is the correct way to connect to a service.

Check out http://developer.android.com/guide/developing/tools/aidl.html

For information how to communicate with a service.

Chris Thompson
1 question, the way i am currently handling this (communication between service and activity), is by making every object and method in my service static.Is this a bad way of communicating with a service?for example every time an activity loads i doMyService.setMainActivity(this);so i pass an instance of my activity to a static variable i have in my service class.And when i want to send message to my server from my activity, i call the static method i have in my service class.MyService.SendDataToServer("hello");is this a bad way to communicate with my service?
aryaxt
Yes, that's a really bad way to do things. The service or activity could be killed without you knowing it. However, if you use the IPC framework build into Android, it'll make sure that doesn't happen or if it does, it'll handle it gracefully. In the long run, using the AIDL interface is much, much easier. It may seem daunting but it's actually pretty easy if you spend a few minutes reading through it.
Chris Thompson
Why the down vote?
Chris Thompson
down vote? I gave "up vote" on your answer and comment
aryaxt
@aryaxt ah yes, thank you! It does seems as though somebody had downvoted it and I was curious why they did that. I'm glad it was helpful!
Chris Thompson
+1  A: 

Check out Intents and the bindService method.

Matt Huggins
+1  A: 

There's two ways you can go about doing this depending on if you want to keep the socket open when the user isn't actively using your UI.

The first is a LocalService it's the same as a regular Service except it runs in the same process as your Activitys UI thread and therefore you can gain a direct reference to it. This is a much lighter method of creating and using a Service than using AIDL. I only recommend using AIDL if you wish to create an out-of-process service that needs to be accessed either by different processes in your application or in different applications. This is most likely not what you need. The LocalService method will allow you to keep the service running in the background and in turn keep the socket open even when the UI isn't in use.

If you don't have the requirement of keeping the socket open and only wish to have access to it from multiple Activitys then you can extend the Application class and register it in your manifest. It allows you to manage global application state. You can put the socket creation and management functions into that class and then provide methods for your various Activitys to gain access to it. The Application class dies when the process dies so it is not suitable for keeping a socket open for long periods of time or during times when the user is not actively using your application.

There is the built in Activity#getApplication() method that helps with this process. You can specify your custom Application class in the AndroidManifest.xml via the android:name attribute.

Qberticus
1 question about LocalService, am i able to keep the service running while navigating between activities? Every activity in my application shows data based on what i receive from the server, and everything is session based (request session, log in, etc) so the socket must be connected all the time.
aryaxt
Yes, you can either call startService, or bindService with BIND_AUTO_CREATE. In that case you'll need to manage stopping the service yourself.
Qberticus