Android Async Task on Splashscreen - android

I'm working on a project. The people who want this app don't want the smartphones to acces the database. So they place xml files online. When the application is first installed I read the xml's in a local sqlite database so they can acces the data offline.
Now the xml files are loaded into the sqlite. It does not take long time to load the xml files, but long enough to place it in een async task. Now I don't know if it's smart to make a splashscreen and run the xml parsing in a async task. Or if I need to do this on an other time?
Does anyone have a solution for this?
Thanks!

First, of course any operations containing network and database lookups should be done in a separate thread (using AsyncTask is a good idea). Then, there are two ways to implement your logic:
If your users can do nothing with your application before the data is fetched and placed into DB, you probably should show some kind of ProgressDialog telling your users that your application is preparing itself for the work.
If there are other options that don't require the data from the Internet, you must run the data fetching operation completely in background, letting your users interact with the application.
Hope this helps.

I think it depends on how would you deal with an error, say, your server is down, or something is wrong with user's internet connection. Splashscreen is fine for loading up resources that are required to show the activity itself, but I don't think it's a good idea to keep user waiting on it running AsyncTasks in background, it could make your application look slow => bad user experience.

Related

Downloading data on Android app first run

I have a language translation app that needs to download some initial data on the first run. When the app is first launched, the first step is to get a list of currently supported languages from the server. The user then selects which they wish to install, and the rest of the tables are then downloaded.
I check if the languages are present in the local db, and if not, connect to the server and download them as JSON. The user cannot do anything until this data is retrieved from the server.
If there is no network connection, a dialog should prompt the user to go to their WIFI settings. If there is a network error with the download, another dialog would then prompt the user to retry now, or wait until later. If the download succeeds, a new Intent is launched to send them to choose the languages to install.
I have this mostly functioning (my AlertDialogs aren't showing), but the question is whether this is the proper way to accomplish this. I've currently set it up as an AsyncTask, but I've seen plenty of posts with responses yelling about how an AsyncTask should not be used when the UI depends on it. Fair enough, but an AsyncTask seems to be the recommended method for downloading data.
Is an AsyncTask the correct way to download the data, or is there a preferred alternative?
How to best deal with this on the UI, as it depends on this data? A splash screen? I'd rather not, but it seems something should be there, and I need somewhere to display the AlertDialogs if necessary.
Is an AsyncTask the correct way to download the data, or is there a preferred alternative?
No, AsyncTask is not a good solution for networking because:
You need a component from where you start this task. Activity is not a good choice because your network request will be tied to the UI and it will be hard to handle screen rotations, etc.
AsyncTask works on a global serial executor by default. It will block all other async tasks in the app until it finishes. So you will have to provide your own executor to avoid that.
The process level would be Background Process according to http://developer.android.com/guide/components/processes-and-threads.html
With a Service you can achieve Service Process which is better.
Use Service instead. You can implement any threading you want inside. You can use a regular Thread, a ThreadPoolExecutor, a Handler, or some third party solution. Service provides you great flexibility.
Regarding your second question, take a look at material design spec first: https://www.google.com/design/spec/material-design/introduction.html
Come up with some ideas and then ask a separate question if that is still not clear.

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.

Puling big data from Web Service in Android efficiently

I am having one web service in which i have list of countries, state and corresponding cities of it. Now I am little bit confused in pulling this data in my Android app. Right now I have only 2(USA,Canada) Country data with me but it might be increased in future. Also I have list of state in this country and yes the cities in every state.
I have one splash screen in my app in which I have written the logic of getting country and on the basis of that i am getting state and then cities for every state. But its long process and if i publish this a user will be frustrated by waiting time. So I don't want to use this way which takes longer time for pulling data. Is there any other way or suggestion for me to achieve this.
All suggestions are welcome.
you should use AsyncTask to perform any web data downloading (and uploading), it works in the background and does not block UI thread, so your user won't notice.
also, it might be a good idea to supply default country/city data along with your application and then download only changes/updates.
Use a Service or intent-service, to download the data in background. and when the download is complete you can display the downloaded data using onbind. service runs on a seperate thread.
You could simple use the system DownloadManager to get the files from your sources and later process them. The DownloadManager will even inform you when the file is fully downloaded.
Take a look a https://github.com/commonsguy/cw-android/blob/master/Internet/Download/src/com/commonsware/android/download/DownloadDemo.java for a simple example code using DownloadManager.

Android: can't decide how to manage background task

I have a task which I need to run in the background in my Android app. It reads data over the network and populates a database. It can take several minutes to run.
Once it's started, it needs to complete successfully without interruption. (Otherwise I'll end up with a broken half-populated database.) I realise I can never guarantee it will always complete, but I want to make it as hard as possible for the system to kill off this task. For safety I guess I will have it populate a temporary database, and then only swap out the old database for the new one on successful completion of the import.
It's a modal operation; it does not make sense for the user to be interacting with the app while the import is in progress.
My first attempt is using an ASyncTask with a Progress dialog to achieve the modality, but this obviously breaks the "don't interrupt" requirement. I could work around the screen-rotation issue with ASyncTasks, but I don't think that goes far enough.
At the moment I'm not sure if this should be an ASyncTask, a Service, an IntentService, some combination of these, or something else entirely. Can you help me decide?
I'd run it as a service and additionally I'd also have a clean SQLite DB on my server populated with the data the clients are going to retrieve so I can generate a kind of signature. Have the clients check for the correct signature of the DB. If the signature is not matching the servers signature then reinitialize the database filling process.
This is just an idea tho. I have no idea whether it'd be possible with what you are trying to do or not.
You are better off with services in that case. The Android runtime will leave it alone working as long as enough memory is available. In the case it kills the service, you can save the state in a bundle, and the system will restart the process as soon as possible, so you can resume the process, if possible for your solution:
Android Fundamentals, Service Section
Then it is easy to communicate with the service, like showing the progress/ notifications etc, using a handle registry like proposes by Mark Bredy in his Android Service Prototype

Best way to get online data and store it in Android

I'm getting hung up on how to handle the data for an app I'm designing. I want to pull a list of items from the net. The data is updated routinely, and I think it would be good to store all the data on the device so the app can load quickly and refresh the data in a background thread rather than have to wait for the network on every start-up.
I think I should make the data available in an XML and have a thread parse and save into a SQLite DB, but I'm not sure if that's the "best practice." Are there other ways that people go about handling this?
The cleanest way (or at least, I think it is the cleanest way) is to implement a custom ContentProvider class that interfaces with your server. You can query the contenprovider and if it doesn't have the data in a local cache (for example a SQLite db as you said) it downloads it from your server and adds it to the local data. Why a content provider? because then you can easily access your data across apps and you have a nice and clean way to get your data when using intents.
Also, I personally prefer not to download data when the app is not running, because it will cost battery life while the user does not actively requests the data.
Sounds reasonable.
If the download of the new data takes more than some seconds, you might want to use a Service. This will allow you to continue the update even when the user has left your app.
Also, think about how you will notify the user that something is going on. Displaying some progress indicator is always a good idea. Otherwise, users might just think the data is not up to date because the app is broken.

Categories

Resources