How do I make an Android background thread? - android

I have an Android application that connects to a database. I would like to write a background thread to continuously update data from the database, using httpclient, and still be able to update the UI displaying the most recent data, let's say, every 1/2 second. How do I do that?

Have a look at AsyncTask: http://developer.android.com/reference/android/os/AsyncTask.html - this is exactly what you need.

That's a lot of data!
If you are displaying and then inserting the same data into the database then AsyncTask will work. However, if you are planning to access your database from the UI at the same time as inserting data from online then you will run into concurrency issues.
I had to use a content provider to provide concurrent access to the database. The beauty of that was that I could then use a syncadapter to handle the data retrival without affecting the UI.

You Can use the class AsyncTask , refer this tutorials to know more about it :) :
http://www.android-ios-tutorials.com/182/show-progressbar-while-downloading-image-using-asynctask-in-android/ ,
AsyncTask Official Documentation

Related

How to perform synchronization in android?

I referred so many links regarding synchronization
http://developer.android.com/training/sync-adapters/index.html
my requirement is to download all the data regarding the user at first time.There are nearly 12 tables are existed at the service i need to download all the 12 table into the android mobile at first time. After that we will modify that local data whenever we click on that sync button we need to send that updated data to the server.
I am following this type of approach: I am sending a json object to android mobile from server side which contains all the 12 tables data.by using that json object i create the tables and insert those data into the local database.Is this correct approach?
Is there any jars exists to simplify this type of requirements?
Please give any suggestions regarding this question.
For downloading the data. You can use AsyncTask in Android. Its runs at background so no worry if you are changing the UI. Here is the documentation
AsyncTask
AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.
Here is the good tutorial on this topic have a look on this
Android Background Processing with Handlers and AsyncTask and Loaders - Tutorial

Fetching data from server (mysql databse) to android application and saving it into sqlite database

I am working on a project in which i have to get data tables present in mysql database on server. Now i have to insert that tables in my android application.
I had successfully achieve that functionality using Json Parsing and asynctask and sqlitedatabase, I have written php code by which i am fetching data from mysql server to my application.
Problem in this approach :
Time : As i am fetching that data from server in my Launcher Activity, My activity starts and take about 10 min to get all data from server. I want to reduce that time, I dont want that much delay, 5-10 sec will be fine
Everytime my application starts it goes in that fetching mode. which result in 10 min of loading page.
I dont want this, I want something that checks for the changes in database present on server, if there is a change then it should start fetching on background itself. I am thiking of service with alarm manager , but i dont know how to achieve all that with service. Should i use asyncTask in service or something else.
I am not sure if that detail is sufficient or not but i will give you detail explanation if needed. Any help in this will be appreciated, If my approach is wrong then I would be more then happy to change my approach to an optimized way.
Use Java threads instead of AsyncTask.In thread you could not access the UI.But performance point of view java thread is efficient for downloading large data.
Here some benefits of thread:
Network operations which involve moderate to large amounts of data
(either uploading or downloading)
High-CPU tasks which need to be run
in the background.

is there any delay time to insert data using Ormlite

I have an Android app and in my login activity I get my data from a webservice and insert it into sqlite database and in my main activity I select this data from the database.
My problem is the first time I use use the app the list will be empty
I debugged the app and when I keep small amount of time between the login and the main activity , it works well but when I run it fast the list will be empty
I supposed that there is a delay from Ormlite library , I don't know maybe I am wrong
How can I solve this problem ?
Note: I used Async task to read data from the web-service and in the post execute method I inserted the data into the database then I called my main activity using intent
I supposed that there is a delay from Ormlite library , I don't know maybe I am wrong
The amount of time that it will take ORMLite to update the database is directly proportional to the amount of data being changed. If you are adding a large number of rows then you should consider doing it as a batch using the Dao.callBatchTasks(...) method.
See here for more details: ORMLite's createOrUpdate seems slow - what is normal speed?
DB operations especially multiple one take there time. That is also somewhere stated in the ORM documentation. So it is the right way to use AsyncTask to do this. I would argue to fill the database not on the post execute method but in the doInBackground method like your WebService call. Ensure to use a WeakReference to the ORM-DB-Connector. Furthermore, give the user a hint about the progress using a combination of Handler and ProgressDialog.

concurrent sqlite access in android application

My application has :
Activity A that reads from sqlite database
Service with notification that writes to the database
on clicking Notification, Activity A opens up
the reading by ActivityA is very small task(in reference to time taken to read)
but the writing by the service to the database is very long(it sometimes takes 5-10min)
now when the service is running and i click on the notification, ActivityA that has to read from the database cannot perform its reading as there is already a service writing to that database.
so activityA has to wait (for 5-10min) to read from database.
on researching further i came across this
http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#beginTransactionNonExclusive()
when i try to implement this in my method inside sqliteopenhelper class i get error as my application uses min api 10. so how do i get this method working for api 10 or is there anyother way to have parallel database access
?
is there anyother way to have parallel database access ?
I think there is no special way how to achieve it. You should use classic Java synchronization for synchronized access to your database.
Most important thing is that you have to make sure you have only one connection to database (you can't write/read from two different connections in the same time). And try to think about an usage of Singleton. In this case (and also in others) it's very efficient and clean solution and you can avoid many problems with access to db.
You mentioned that your task can last 5-10 min.
In similar cases every user should know that you are performing some calculations in the background e.q. show some progressDialog, progressBar or simply start animation of image.
If you are showing some data for example in List this is good reason to use lazy loading.
Have look also at these articles:
Android Sqlite Locking
Using Singleton design pattern for SQLiteDatabase

Activity won't start until data is pulled from MySQL server

I have an Activity that displays a text based on data pulled from MySQL server. The problem is that the Activity won't load until data is pulled, which sometimes takes some long seconds or even doesn't load at all, and in the meantime the users gets a black screen.
I tried to pass the mission of getting the data from the server to a service, but also it waits for pulling the data and only then shows the layout of the Activity.
I also tried to make an activity with fixed text and then call the Activity that pulls the data from the server, but still the program wait for the data.
Can you think on a creative solution for it? or maybe a non-creative one as well :)
you can use asynctask for this:
http://developer.android.com/reference/android/os/AsyncTask.html
or you can show a waiting dialog to user until you get your data(do it in separate thread).....
or you can implement a splash screen and there you can fetch data.....
You need to do it inside another thread. Try using AsyncTask class.
The delay is probably due to the call to fetch the data being done on the main thread, also called the UI thread. Processes which take any significant amount of time, and by that I mean even a second or two should be done in a seperate thread. Android provides a class called AsyncTask to help make threading painless.
You mention you tried a service but did you take a look at an IntentService? (Can't link it yet but it's on d.android.com.) I like using them for these kind of tasks cause they handle the threading for you (like an AsyncTask) and it separates concerns better. The IntentService then sends a broadcast message that the activity picks up indicating that the data is available or not. Store the data locally in a sqlite db or as a json/xml file.

Categories

Resources