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-
Related
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
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 am creating an app that requires a local database to be updated every time there has been a change in the server database. I want to run a service that runs every 2 minutes and updates the local database using ORMLite.
Would an IntentService be the best route to do this? Also I am getting mixed results on my searches about if all services are asynchronous or do I have to call a asynctask in Services other than IntentService.
Any suggestions on which direction I should go would be greatly appreciated!
I think what you need here is a Bound Service
It helps you deal with client-server model, where you main application can take advantage of background service. This service will do the job for you in background. With IntentService, this concept becomes a little bit different, you should handle this when you have a spefic job needed to be done in a short time ( not periodically like yours).
And because of frequent updatings , I think you should keep a socket connection to your server
to receive pushed updates from it ( I think some mail clients do this way).
Hope this helps.
I have a service which is is responsible to send requests and take responses over network. I am planning to use it also as an API . So other applications on device can bind to it, send requests and take responses.
1-Is this a proper way to provide an API to other apps?
2-Should I use a Messenger or AIDL ? Messenger seems simpler, but network operation can block a request, so using a single queue for requests can be problematic !
3-Is it a good idea to use same service for both network operations and as an API for other apps ? I can create a separate service for API which binds to network service, but this will bring extra message overhead and code complexity !
According to me you need to create a service separately that can be used by both of your applications. And you need to use BroadcastReceiver to start your service as and when android system boots. So that any of your application can use that service.
Creating a simple service is the best idea. You need AIDL for that so that you can transfer the data between service and application with ease.