My application can now synchronize some data to a remote server, and at the moment, it happens explicitly when my user clicks a button, the app shows a ProgressDialog and does the network operation in background with ASyncTask.
Now my client requires that the synchronization should happen on any data change. I cannot do it on the "foreground" like it's now, because it usually takes 5-10 seconds on mobile data connection, the app has to collect data from sqlite database, serialize it to JSON and send to remote web service.
Do I have to implement a Service that will receive a message from the application that it's time to do the synchronization? Or could I just move my ASyncTask to Application class and do the operation from there?
A service would be the best way to do this.
Related
I am collecting data from different activity and sending to the server periodically, also if the user is in offline mode am using Sqlite to save data and again sending that whenever the internet available,
I have to schedule this transmission from many activities
which is the best for my requirement asyncTask or IntentService ?
I have to divide the data as chunks if huge data present in Sqlite, please suggest some algorithms to do this.
1.asyncTask is used lock the ui thread and ruining for long time process such as connect the web server.more details
2.IntentService is one kind of service it use handle asynchronous requests.more details
you can see this tutorial for send data to web server sync
Intent service is the best method, also it avoids context leak !
My app has a UI and a connection is made to a bluetooth device which is periodically sending barcode scan data to my app. I then want to cache this data in a sqlite db and have another process push this data up to a web server.
I have managed to get the UI and bluetooth scan process separated by using an Intent Service for the scanner component... The thread in the intent service connects to the bluetooth device and loops endlessly pulling in new scan data as it comes... communicating with the UI via broadcast messages as it needs to.
So now I need to handle storing the data in a sqlite db and pushing it up to the Internet.
I'm thinking I can insert a db row directly in the intent service loop I already have working for the bluetooth data... would I do that by firing of an async task or something like that?
Then, would I have a completely different intent service running and looping endlessly checking for new records to be processed and pushed up to my web server via an http post?
I guess the main reason I'm thinking of using intent services is that they seem to keep running even if I lock my phone and put it in my pocket... has worked so far for the bluetooth barcode scanner... can scan away happily with my phone locked and in my pocket.
I also need to handle the reality that internet won't always be available... hence the sqlite db... kind of like a safe buffer to store data until it can eventually be pushed up to the Web server.
Am I going down the right path? I'm really new to Android development and even after much research I'm still unsure about my approach.
You can store data or communicate from IntentService onHandleIntent() directly. You don't need to run it in separate thread, unless you want reading bluetooth to continue ASAP.
Handling everything in an infinite loop smells. Also keeping service alive depends on few factors. If IntentService dies, it won't be restored because by default IntentService.onStartCommand implicitly returns START_NOT_STICKY, or START_REDELIVER_INTENT if you call setIntentRedelivery(true) on this service. Check Service javadoc for more info.
If you can scan bluetooth periodically then I would consider kind of scheduler. For that you would probably need to implement Service, not IntentService and handle background thread yourself. Alternatively, you could use a Timer. These are more hints, not ready solution. Since you asked about direction, I assume you will investigate solutions yourself.
Depending on handled data you could separate DB operations and network to separate services. Think about them as modules which are decoupled. You will benefit maintaing this code in the future and in case one service goes down due to any reason, the rest will keep working. It depends on data size because it's not a good practise to push heavy data between service/activities(data is serialised and deserialised every time it is sent).
If DB is just a buffer/queue then maybe use it directly after reading bluetooth data. In other words queue data for sending. Create second service for HTTP communications. Don't push entire data to second service, just inform it about(knock the door :)) and let HTTP service access DB by itself. I would wrap DB in ContentProvider and access it from services.
There are probably different techniques out there too, but that's what I thought about it in the first place.
Detail:
I have developed an android native application on ADT.
App performs ADD , Search and update operations on SQL database hosted on Windows Azure cloud platform.
All the operations are performed by calling a Mobile Service built on Windows Azure Cloud platform.
Problem Description:
Every time I want to ADD a new record via UI of app, my app calls mobile service and get hang till the operation completes.
I want to do this operation but do not want user to wait for it to complete.
Additionally when there is no network, user addition should be kept in queue for later addition.
Ideal Scenario: I want to display Addition operation in pending status to user and make that as a background process till the time it completes.
Note: There can be many ADD operation going on by multiple users. Need to keep that in mind.
Please suggest options to do this.
Free to ask questions in case of any query regarding problem statement.
Thanks
Anshul
You have to use AsyncTask to do work in the background and update the UI for the User or Pop a notification in OnPostExecute which means background work has been done.
For Queuing when No Network:
You would have to user a Service with BroadCastReceiver which listener for Network-Connectivity. Once the BroadCast Receiver Receives that Internet is Connected, You can then start calling the mobile service.
But Let me add that the Queued Data has to be Saved so you would have to create a SQlLite database or save the Data to a file on the Phone, which you can retrieve the data from, when the BroadCastReceiver is notified that connection is back
I'm developing a simple multi-player Android game that communicates with its Game Server using Java Sockets. And to create non-freezing UI, I decided to make Service class in Android app to communicate with server over the Socket connection. Data exchange is done in JSON format.
Now the thing is, during entire game play, a single Socket connection will persist, and data exchange will be done from-and-to the server, but based on events happening on game client running in Android.
Eg; When user taps a button, some data is sent to server and server responds back with some data, until then, client will wait, when data arrives, UI on the activity will be updated, and client will again perform some action, and again that data will sent to server, and so on.
While the client is waiting for data from the server, I'll disable the controls on Activity such that user doesn't perform any actions until data arrives from server, and I'll also show waiting dialog on Activity to let user know that app is not frozen and is waiting.
Now, as Android service primarily works with onCreate(), onStart() and onDestroy() methods, I believe it executes something in background without any user intervention. So in case of my app where I need some input from user (or wait until user provides it) to send it to server, how I'll be using Service class in this case? Also, is there any other option I have apart from using service in such a scenario.
Also, note that I have two activities in my app which will be communicating with the server using single Socket connection.
I have written an application that queries a web service I wrote (which returns JSON data). My app currently processes the web service call using an AsyncTask and updates a TableLayout with the data it receives. I want my app to regularly (every second or so) call this web service and display the data in the DB, as it is continuously being updated. How can I go about doing this without having the UI thread block?
Currently the way things work is the user presses a "go" button, and the AsyncTask displays a "loading" dialog while the request processes. I would like for them to press the go button once and have the data refresh in the layout. I'm not sure what the best way to architect this is.
I wouldn't recommend that you create a new AsyncTask every second since this is going to result in a lot of object creation and corresponding memory collection.
What you can do instead is create an AsyncTask that after each request returns from the web service updates some internal data structures and then calls publishProgress(), waits the appropriate amount of time, then makes a new request to the web service. In onPublishProgress() the code should then get the new information from the request from whatever internal structures are being used (don't forget to use a lock here to synchronize access) and refresh the UI.
You'll also want the AsyncTask to have a method or variable that the Activity can call to tell it to break out of the loop.
You can use a Handler which can initiate a new AsyncTask request after every second.