What should I use if I need to periodically poll a REST api to see if the data in my listView has updated from the server? I though about using a ScheduledThreadPoolExecutor inside a IntentService but It might be overkill? It has to be done in background and if the task is killed when the Activity that contains the listview is destroy, that'd be great too.
What is the modern way to do this now in 2016?
You can write a SyncAdapter by extending AbstractThreadedSyncAdapter. This is well documented here:
http://developer.android.com/training/sync-adapters/creating-sync-adapter.html
Instead of polling the REST API you can use a BroadcastReceiver to receive messages from the server when the data changes. GCM provides the components you need to make it work. See here: http://developer.android.com/training/sync-adapters/running-sync-adapter.html
Related
I have an Android app that has a service that handle Firebase messages. So far it's working for me and I receive messages properly even when app is in background. What I need is somehow update app content when a message is received. That mean use network to contact backend and download fairly long JSON data and put them in database.
I have model in repository which I use in Activity. Now I am considering whenever I have to somehow call activity and refresh data but I think activity is somewhat dead when app is in background? So I don't know if it is even possible.
I can download data inside the service but my understanding of service is that it shall be lightweight and all the model and network calls seems to be a bit overkill for the service. Am I wrong?
What is ideal solution considering standard android architectural approach? Thank you.
I required to send custom object to server periodically via REST API.i know there are various way available like Bound service,Job scheduler can anyone any guide or example on how to make REST API call from Android's Service or from Job scheduler Periodically furthermore How to handle scenario when service get killed by android then how to restart it for sending data.Any help will be more helpful for Novices like me.
I strongly suggest you to take a look at :https://developer.android.com/topic/performance/scheduling.html Firebase job dispatcher you can schedule your task as your requirement and take a look at this example:https://github.com/firebase/firebase-jobdispatcher-android#user-content-firebase-jobdispatcher-
I want to get data from the server and store it in a cache regularly, even when the app is closed. I am not sure what is the correct way to do it. I have listed down the possible ways I can think of. Please let me know the correct or the best way to do it. Really appreciate any help.
Create an Activity and set a repeated alarm to call a service. The service should connect to the server and download the data in cache.
From a fragment, check the last time the cache was updated and then if the data is out-dated, connect to server in a background thread and update the cache.
You can Directly Use IntentService for Frequently Updating Data
IntentService is a subclass of android.app.Service class. A stated intent service allows to handle long running tasks without effecting the application UI thread. This is not bound to any activity so, it is not getting effected for any change in activity lifecycle. Once IntentService is started, it handles each Intent using a worker thread and stops itself when it runs out of work.
IntentService would be an best solution, If you have an work queue to process. For example, if your application using analytics you will likely to send event name and related parameter to your tracking server for each user generated event. Although each event means a tiny piece of data, creating networking request on each click will result an overhead to your application
For implementation : Updating Data from Server Using Intent Serive
Do take a look at Android Sync Adapter Framework.
https://developer.android.com/training/sync-adapters/index.html
Hope this helps.
I have a website that sends and receives documents. I was thinking of building an Android app that notifies the user if a new document has been received, and displays document details if the notification is clicked. It doesn't have to be in real time, it could update in interval of five minutes or something.
What is the best way to update the Android app of changes in the website? I'm new to Android and I'm not quite sure where to start. I've heard of Services, BroadcastReceivers and Alarms, but I don't know if those are the right ideas.
Update: How do I update my Android db from my web db within an AsyncTask in my BroadcastReceiver? I'm worried I might have a "leak error" which sometimes comes up with my AsyncTask.
You can try to implement GCM or as the above-mentioned, work with an AlarmManager or the more efficient JobScheduler (requires API level 21!). Avoid doing heavy work on a BroadcastReceiver. Instead use the Broadcastreceiver to receive Alarms and start a Service in background. You may also have a look to WakefulBroadcastReceiver which holds a WakeLock for you. The Service could GET data from your webservice by using a REST architecture and update it's local database. Retrofit is a powerful open source library for a REST architecture. If there are new database records, you can inform the user by a Notification. Don't forget to check basic things like not starting the Service if the device hasn't got a network connection or to stop the Service after the work has been finished. I personally recommend to learn the basics first and then go to advanced topics. Good luck and pleasure.
I'm a beginner in android development and I'm trying to implement an android udp client, which connects to a java server and sends/receives some packets from it.In this process it collects some data (like round-trip delay etc), which is used to measure the QoS of that particular network. I have tried implementing the connection and sending/receiving data using Java Threads, but the application crashes, and hangs if i try to use more than 2 threads. So I'm looking for alternatives. While going through this site as well as some other links I found that in android multiple threads can be implemented using AsyncTask, Handler etc. Also I found that the Service class also helps to run a background service in an app. Please suggest which approach among these would be the best to achieve my purpose.
Thanks in advance.
You can use AasyncTask to do this and as you mentioned service may be useful too, where u can let your application do whatever it wants in background , if user needs to use application by its interface then AsyncTask must be used to avoid Crashing
There is not one right answer that can be applied as a broad stroke to how to do Android multi-threading. There are a few different ways to approach it based on what your specific needs are.
Any long running, blocking call, in Android will result in the application crashing.
The most common solution is to use an AsyncTask though. For example, when I want to make a call out to a web API endpoint for some XML data within an Activity I would in this case use an AsyncTask and kick off the calls from within doInBackground.
This is not an appropriate solution though if the wait time is longer, or possibly an unknown wait time. Or in a situation where there will always be waiting such as a message queuing service. In this type of situation it may be best to write a separate app based on extending the Service class. Then you can send/receive notifications to/from the service from your primary application in a similar manner to how you would communicate with a web service.