Uroš Podkrižnik
Uroš Podkrižnik

Reputation: 8647

Android: sending request from service

I have an application with list of data that I get from server with http request. Now I want to make a notification when new data is available and I assume that it can be achieved with service.

Is that a good practice? Is there any limitations for number of requests made from service?

What I want to achieve is something like gmail application. When I get a new email, notification is shown.

I want my app to be as up to date with data as possible, but I understand that making requests every 5 seconds might be too much.

I am open to all alternatives and various ideas how to do that.

Upvotes: 0

Views: 585

Answers (1)

cgr
cgr

Reputation: 4636

Not sure if you really need to pull data every 5 seconds. Of course, it is too much. You have two options:

  1. Use GCM as @duynt suggested in comment. Follow Try cloud messaging for Android if you've never used it. This way you don't need to worry managing your service locally but whenever there is a latest data available, you will get notification so you can place request to get that and update in notification.
    GCM needs An application server that you must implement in your environment. This application server sends data to a client app via the chosen GCM connection server, using the appropriate XMPP or HTTP protocol. Take a quick look About GCM connection server.

  2. For any reason if you would like to pull data from your local Android Service component, you can still do that. But 5 seconds frequency is really high. As majority of the times the device is in sleep mode, you have to wake up the device then send request to pull data. Waking up device every 5 seconds means a battery drain along with consuming data.
    If you still want to proceed with local service option by increasing the frequency, make sure you follow How to use http in sleep mode and implement it that way otherwise it wont work in deep sleep mode.

You have to make a decision now.

Upvotes: 1

Related Questions