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.
Related
This may be duplicate question but am still having doubt am a beginner in android application i have a couple of doubts my primary doubt is:
I have made one application which will communicate with server when network available it will work as it is. when network is not available data will save in sqlite and later when network is avail need to sync that data to server how can i achieve this.
Whenever there is new update is made with server need to get notification how can i do this
For this one which will be the best approach syncadapter or server or intent service with broadcast receiver which would be opptimized solution for the above requirement
These are all my doubts i would be very glad if someone helps me !!!
If you want an Android app to be notified when something happens on a server you control (without having the app to constantly poll the server to ask for changes), the usual solution is to use Google Cloud Messaging to allow the server to send a notification to the app to tell it to refresh data.
It is kind of complicated to implement, but is the best way to do what you want and is standard practice for mobile apps.
If you need to know when the network becomes available, to reach your server for synchronization, implement connectivity change listener, as discussed in this question.
This does not allow to send messages from the server easily, but if the server messages are not of high urgency, maybe you can simply check for them periodically.
This would allow to use less Google specific infrastructure and change the cloud providers easier.
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.
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.
To start of, i should mention that i'm a newbie in Android (Not that much experience in Java at all tbh), so be easy on me.
I am making an app that continuously pulls data from a server, and then returns data through a http post request. The question is, what is the best way to handle the actual pulling from the server? Should i be using AsyncTask or create another thread and let it run on that? Are there better methods for this purpose?
I will be pulling data every 5 minutes. (I am aware that this will drain the battery very fast, and i should definately be using Androids C2DM framework. But i have no experience in it before and i'm on a deadline, so this'll have to do until i have time to learn how to implement it.)
I'm grateful for any advice!
As an alternative to C2DM, you can do the persistent TCP connection between your device and the server. Then every 5 minutes your server can push a tickle to the device. Upon being tickled, the device can request the information via Http post.
Here is some sample code on how to do that.The connection stays open in a background thread even after the app has exited
Creating and Managing a persistent TCP socket: http://openmobster.googlecode.com/svn/trunk/cloud/android/connection/src/main/java/org/openmobster/core/mobileCloud/android/module/connection/NotificationListener.java
Full Disclosure: I am the Chief Engineer of OpenMobster and I wrote this code. Please feel free to use whatever you like or just get an idea if thats what you need
Thanks
Do you need to pull the data in background (even if your app is not "opened" and the android device is sleeping)? I suppose thats what you want because you mentioned C2DM. If so..the buzzwords are AlarmManager (with repeating time)/BroadCastReceiver and maybe NotificationManager to notify the user. With AlarmManager you schedule your events (every 5 minutes) and with BroadcastReceiver you receive those events and do what you want to do every 5 minutes :)
I want to create a background service which continuously check that is there any message that has come from server.
I am trying using AsyncTask but I am not getting how can I know when any message will come or has arrived.
Please Help.
I take it that you are polling at a set inerval to your web server to figure out the state. Simply read the response from your web server and take action.
Read this post
Example: Communication between Activity and Service using Messaging
Alternately try using the Cloud to device messaging.
If you want to go really large scale use XMPP.
I got the way from here.