I need to open and keep long term connection with server to send messages, recieve response. Also sometimes server sends information without user request, so android device should listen to the server and react.
There are AsyncTasks, where I can implement socket connection, but main problem is that I know only one way - to send request and recieve response once. Then AsyncTask (and connection) is closed.
I have also read about services (that I never used).
Is it possible to make long term (1-4 hours) connection with server that keeps connection alive, listens for user commands (for example, need to send data to server when button is clicked) and recieves response or requests from server (and then changes UI).
Will service (and connection) be killed when phone fall asleep, needs more memmory or other? Is it big cost to the battery?
Maybe there are other ways? Thank you in advance for all your answers
P.S. sorry for poor english skills, hope you understood :)
You should probably use a Service, running in background.
Also, you really need not keep the service always alive with a network connection. You can opt for Google Cloud Messaging, which supports 2-way communication via the XMPP protocol. Using this protocol you can:
Receive notifications from server, start the service and do necessary processing.
Send notifications to server, upon which server does any necessary work.
These notifications are short 4kb messages , so they are better used as "commands" of a publish/subscribe model, which initiate other network heavy connections such as uploads and downloads. Rest of the time the service can be inactive to reduce resources consumption.
According to Android API Reference
"A Service is an application component that can perform long-running operations in the background"
And yes it consumes battery and you have to stop it by yourself:
" It's important that your application stops its services when it's done working, to avoid wasting system resources and consuming battery power"
So I think Services fit your needs.
If you need to communicate with the server when you want to send data to the server you can do it and wait for answer. If you need to send data from server to the device then take a look at push notifications.
Related
I want to implement a long running service to receive push messages from emqx server (even when the program is not running). If I use android Service or JobIntentService, it will be necessary to display a notification which is not intended. If I use WorkManager, The minimum repeat interval that can be defined is 15 minutes. For some reason we do not want to use Firebase. Is there a solution to this issue?
"Push Notification" means "the server triggers your App", so there are two main methods:
use native Android Firebase Messagging create
a persistent connection between all Clients and your Server
First option uses a single connection which is managed by Android for all (or most of) Apps, so the energy compsumption is very low and Connection/Network events are managed by Android itself.
The Second option requires heavy work about Connection/Network re-connections and some work on Server-side. Your server should hold and manage many Client connections for how log it's needed. Moreover the Server should know Client's TCP/UDP handler to know which Client to send message at.
This way drains more battery energy than the first one.
If you intend "Push Notification" as "polling the Server to know if some Notification is ready", then you're misunderstooding "Push" word and you can ONLY create a persistant Service WITH notification icon (this behaviour is mandatory starting from SDK 26).
As you mentioned, in new versions of android, there are many restriction on using background services. Therefore, I suggest you using a hybrid approach if you can. Your server informs the client for new messages using google-fcm service and whenever the client received it, on WorkManager establish a connection to the emqx broker in order to get the actual messages. In this approach you can connect to your broker whenever the app is on the foreground and also you do not send your messages through the fcm but using it as an alarm to clients for new messages.
I have constructed a background service in which, every particular interval, a request to the server is made which - in successful cases - a JSON response is parsed.
I am using Retrofit API to handle this situation, but I have come to a point where I wonder if this is the most efficient solution.
Should I use sockets or some other kind of API? Is this memory efficient?
Yes, Socket is the best way for continues connection or network calling, And also faster calling if you are using socket,
Also throw socket you can make like live connection, But for continues connection in background you need to create service and manage properly.
Push mechanism is the most efficient way than Polling(network request to server in intervals), provided the server supports push mechanism.
You can refer this for GCM.
https://developers.google.com/cloud-messaging/gcm
But in case if polling is the only way, you case use any of these
Service
Handler
AlarmManager
https://developer.android.com/training/scheduling/alarms.html
https://developer.android.com/training/best-background.html
I'm try to make my Android App that send an HTTPRequest to a server repeatedly even when the App is not running (much like chat).The server will return a JSON file and if there is any update at the file, the app will send a notification to the user.
How can I do that?
Thnks.
From you question i can only understand that you are polling from server. Repeated hit will drain your battery very fast and will exploit user bandwidth also. With this kind of mechanism there are changes that your app will be uninstalled very soon.
So, what i will suggest use push notification ,if there is any update let server tell you. There is no need for client to ask for it.
One more way but complicated both at server and client side is you can open your own TCP socket and can then you can share as much data in both direction.
I can suggest you to go with push-notification for easy implementation and do right thing.
You have to use Service as explain here :
http://developer.android.com/reference/android/app/Service.html
Only service could run in background when Activity are not visible.
My android app has a ContentProvider and stores data (strings) in a SQLite table
These rows need to eb sent up to an internet server
I want this data to be uploaded in the background when the remote host is available (WiFi on for instance)
The data should be sent asap (when network available of course)
without interfering with my app's ui
It should send even when my app does not have the main focus (user may be sms-ing or using the camera)
New data could be added to the table (out queue) every 5 seconds
Q: Should I use a background service to send the unsent rows?
It seems too frequent to use the "Alarm Service", but I could be wrong
Battery life is my main concern, but secondly is that it needs to send as soon as possible (or shortly after internet available)
Android Studio
targeting API 17+
A background service would be best to handle the request and save battery life. You could either check the connection state and send yourself and just let the service handle when there is no connection, OR better yet just use the service to always send the data and let it decide when to send it.
As for the service, see this question: How to be notified on wifi network status change?
The idea would be to use the code in that answer to implement a broadcast receiver in your service to detect when there is an available connection. When that connection is available then you could send the data.
i have applied the demo code of GCM for server side on appache server and for client side on android device and it is working great; but i need to activate this service in my application as follows:
i have a database on SqlServer and need to automatically send a push notification to android phone whenever some certain data gets modified, i think i should use an after update and after insert trigger to do this, but i don't know how to do it.
any help will be appreciated, thanks in advance.
You should not do it from a trigger. Adding the latency of a GCM push to each table update will quickly bring the performance to unbearable lows. You have to decouple the trigger from the GCM call, and the best way to achieve it is via a queue.
You can use a table as a queue and have an external process monitor the queue and handle the GCM call.
You can use Service Broker and deliver the GCM call from an internal activated procedure or from an external process that monitors the queue
You can use MSMQ and monitor an NT queue from an external process.
My recommendation would be to go with first option as is the simplest and has the inherent robustness of simplicity. Read the linked article to understand what is required to use tables as queues, and do not cut corners.
All options still rely on a trigger to enqueue the notification, but it will be a local enqueue, not a GCM call.
I'm sure someone will think of the naive solution of invoking GCM from the trigger itself using SQLCLR: don't do it.