Keep socket connection between activities on android - android

I'm developing an application on android 3.1 and I have an Activity A that has a subclass extending from aSyncTask, this subclass create a socket and connect to a server. All my communication is good. I received messages and send commands to a server, but when I got a specific command I have to start a second activity (activity B) but I can't lost my socket and the establish communication with the server, plus I have to still able to receive and send commands from activity B to server. How can I do that??
Any help please!

My approach is implementing a service and move/centralise all your network connection code into service, for all activities that want to use socket connection, bind your network service in onCreate() then after finish unbind it in onDestory()

According to Dianne Hackborn (Android engineer), the recommended practice to pass network connections between activities is to create a singleton that any activity can access and manage the connection from there. See here and check the first post by Dianne.
The Services page on the android developers site (side note under the 'Basics' Section) also mentions that you should only use a service if you need to run code that needs to continue execution while your application is in the background.

Related

Android Service for communicating with server

I need the service to have a steady connection with the server.
Activity and service should have bi-directional communication.
Here are the options I know
Use intent service
Extend Service class ( make it run in a different process ) and communicate using Messaging
My thoughts:
Intent services are nice, they run in their own thread, but they stop when the task is done. I don't want that. For example, if for some reason the connection is interrupted it should try to reconnect, instead of exiting the service
Extending service class makes sense. I have previously worked on a project that used Messaging for communicating with the service, the problem here is that as the project grows, it becomes hard to manage the communication.
I'm looking for an approach, where the service runs in a different process or thread, should start again if terminated for any reason (something like START_STICKY).
Also it would be nice if the communication is easy and something like Otto or EventBus can be used.
Edit : So the question is, what's the ideal way to implement it, which saves me from dealing with Messages and gives a STICKY service.
Use the Retrofit Library to make API Calls

Communication with a service from different applications

I have a few applications that connect to a network service in order to call a few messages. As the communication with this network service is planned to change, I was thinking to implement a service that will deal with the communication with the network service, and the applications will just send intents or messages to this service. My idea is to have this service standalone, started after boot up, so it needs to be started service. It will connect to the network service and will wait for commands from the applications. So far, I found that Messenger is nearest to what I need, but I think it can only work as a bound service.
Is there already something I can use for my case?
If not, then am I correct that I would need to:
implement a started service
use HandlerThread to handle the input messages - my preferred way is to use intents, but so far I don't know how to do it
So as pskink pointed out, the way to go is using Messenger.
I used com.example.android.apis.app.MessengerService (from Android SDK samples) as an example of such service.
The service is started on reception of ACTION_BOOT_COMPLETED and (on my device with Android 4.2) when the first client tries to bind to the service, the service's onCreate and onBind are called. The communication occurs by sending Message objects and I have defined an enum with the supported messages listed (so I am not using AIDL directly).

How do different Services share one network connection?

I'am developing something like a Push Notification SDK, when users integrate the SDK to their application, the SDK create a Service and receive Notifications through a TCP connection.
Now here is the question: Assume that many applications will use the SDK on the some android device, all of the application need to receive their own notifications, if we create TCP connection in each Service, it will cost too much resources and battery power, so I want to use a main Service(maybe the first Service start by SDK) to set up TCP connection, the other Services reuse the connection by some kind of IPC. But I don't known what kind of techniques is best to use.
Is there anyone who can give some advice?
thanks~
Finally I solved the problem this way:
Make a public Service(android:export="true") to set up TCP connection to the server, when the APP starts, first check if the public Service exists, if not, call startService(). Then call bindService() in the APP. To communication with the public Service, use a Messenger class:
http://developer.android.com/guide/components/bound-services.html#Messenger

Android singleton between service and actions

I am working on a project that. It will connect to a remote host using tcp sockets from a service. And interact with it for getting and sending data.
The flow, I have planned is;
a singleton socket class. Which returns a connected socket refference;
an activity to ask user for remote server's ip and port.
a service triggered(intent) by the activity with the parameters of ip:port.
service will get the socket using singleton socket class.
and then service will read a data; parse it and show user for user actions in a new action window ( intended).
after user completed the action; result will be sent to the server. and new request will be read.
problem/query is here;
when you come back to the service for second data read operation; will the socket be there ? (or garbage collected )
because after data read operation started no new connection accepted.
will the first service die after calling the user actions activity ?
what happens if I call the service using startService from an activity, a new service created or the existing before started service called back again ?
how can I let the service live for ever unless I said it to die.
Sorry, If I am asking silly questions.
Here's how I see it:
when you come back to the service for second data read operation; will
the socket be there ? (or garbage collected ) because after data read
operation started no new connection accepted.
If the object is static and has the socket as a member variable it will most likely be there, you should have safeguards in your single pattern to shield from this problem (if single==null){...}, you could also try managing it by overriding the Application class and adding a factory method in there. You need to make sure the socket is open and closed correctly and not just left hanging as this could create problems
EDIT:
Whether the actual socket is still open will depend on the timeout of the socket
will the first service die after calling the user actions activity ?
Services need to be told to stop and so it will not die
what happens if I call the service using startService from an
activity, a new service created or the existing before started service
called back again ?
If the service is running, the it will not create another, it will call the onStartCommand() of the running service
how can I let the service live for ever unless I said it to die.
This is default behavior

Android - Shared networking between activities

I am developing an application which utilizes a custom network protocol over TCP. Several tasks within the application use different activities however each activity requires the networking enabled (since each activity has to send/receive some custom packets over the network).
So my idea is basically to
At application start - create a service to handle networking ( I read somewhere that I should do the networking in another thread in this service to prevent ANR)
When a new activity is run, it binds to that service. During that time it sends/receives data from the service. When the activity ends, it unbinds from the service.
I would like to know if this makes sense.
Also, I understand that I can send data to the service (to send over the network) using the aidl interface, but how would I receive data from the service? Polling through the aidl seems to be the only way I can think of - which means I would need another thread in each of my activities to prevent ANR. Surely there is some better way.
I appreciate your help and am open to suggestions.
Actually I've been reading more and am beginning to look at callback methods in the sample code provided here. After reading it more thoroughly I have a better understanding of the services and AIDL.

Categories

Resources