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
Related
I have an desktop application that receives data via websocket (server) and an android client websocket sending data (PC input can be controlled via android). Both also do the other job (sending/receiving) but pretty rarely. I want the client to run as a foreground service, so I can send data like clipboard from android and receive e.g. music (artist, etc.) from server.
My question: how can I build a foreground service that holds the websocket open while also maintaining fast (20 requests per second) communication with the activity, including callback? I used a Singleton (Kotlin object) before but with the foreground service that sounds even worse than with the activity open. I don't seek a coded solution here, just a plan on how i can pull this off.
Thanks in advance and sry for the bad english.
I think you should try to use Bound service - the Activity binds to the service as described here https://developer.android.com/guide/components/bound-services
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).
I am working on an android project that requires communication against a BLE device.
as for now I am quite done - however I have one thing I cant solve -
I want to use a service to coomunicate against the BLE device and communicate
with thata service only by sending and recieving broadcasts.
my question is - for some reason when I try to connect to the device inside the service
by making the service extend LeScancallbak finding the device and connecting to it takes much longer and also the BLE GATT server is null (since the gattcallback is never called) however if I bind the service to activity and implement that LeScancallbak in the activity its a metter of acouple of milliseconds -
Did anyone also had the same problem or possibly have an answer to that strange behavior?
I want to use only the service to communicate against the bluetooth device since I want to stay connected to the device even when the application UI is not running (i.e. leave a background service) plus it looks more logical that connecting every activity to that service upon changing screens.
In case anyone have a better way to achieve that, I wil be happy to hear about it.
BLE has a lot of issues on Android, and implementing a service like you want will be difficult. I would recommend integrating the BLEService library produced by Ratio. The source is available on GitHub: https://github.com/RatioLabs/BLEService
I'm writing a small chat client for android. So the behaviour I want is, that my application is listening in the background for new messages and shows them in the notification area (only the icon of my app). Listening for incoming messages should be a really long term thing - maybe over days after closing the ui thread
My question is: How to do it? Should I have a service with it's own process? should I start an AsyncTask for the Listener? Or are there much better ways to do it? And if I use a new Service Process, do I need to use AIDL?
How to do it?
Have your chat server use GCM to push chat messages to the device.
Should I have a service with it's own process?
No.
First, it would not need to be in its own process.
Second, this would require you keep the service running all of the time, which many users dislike.
Third, this would require you to keep the device powered on all of the time with the WiFi radio powered on all of the time, which users will not appreciate. The exception: if you are running on a device supporting mobile data (e.g., a phone), and you are very very careful, you can maintain an open socket connection to a server while the device is asleep. This is difficult to get working properly.
should I start an AsyncTask for the Listener?
Probably not.
Or are there much better ways to do it?
Have your chat server use GCM to push chat messages to the device.
Or, possibly, change your chat server to work on a queuing model, so you can poll periodically (on a user-configurable interval) to pick up available messages.
And if I use a new Service Process, do I need to use AIDL?
No.
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.