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.
Related
I have a use case where I want to download some files from the server and store them locally before starting another activity that is dependent on this file. This kind of design can be found on karaoke kind of applications where clicking on one song would
Load the required files from the server
Once the download is finished, open the required activity.
Let us assume that my app is a karaoke app. My question is how to design this flow. Basically, on clicking on one song, I want to show progress on the screen and when the download is finished, I want to move to the activity. Another requirement is that once I am inside the karaoke activity screen and playing a song, I have an option which leads to loading of another lesson. If a user uses that option, I want to again download the required files. Based on this requirement, should I:
Have the file loading thing as a separate activity?
OR
It can be used as Fragmentinside the activity where I choose a particular song. In this case, once entering the karaoke screen, if I choose an option which leads to downloading some files and reloading of this activity, is this the best design?
I would recommend two different approaches depending on how long you plan on keeping the data that you've downloaded. If it is single use than a bound service would be ideal. However, if you are planning on keeping the downloaded content for more than a single use, I would recommend you use a content provider and possibly a sync adapter(Depending on how frequent/predictable content downloading is). This combo would help guide you into not having to think about the 'design' as much(Since it is pretty standard at this point), plus it would provide a lot of features that you may/may not find useful: you can make your internal data 'public' via the content provider/authority(s), you can make an 'account' on the phone associated with your app so that the user can manage its syncing via the sync manager(actually via widgets/apps using the sync manager, but still), and most importantly a set of clean(ish)/standard means to interact with it/propagate UI, etc.
My simple version would be an Activity that spawns either a async-AIDL service with callback (which is in my opinion the only way to use a bound service) that would allow you to asynchronously design your 'starter' activity, its "currently downloading" spinner (which can get progress updates via the callback if you design it that way). Then once the download is complete then send the results (via a parcel file descriptor in the Intent's bundle) to the new activity that makes use of it.
However, if you are planning on using the data more than once, I'd recommend downloading the content like you did above, but then also store it in a content provider for easy access later. Providers also have a lot of nice associated functionality related to cursor loaders, etc. that will help keep a list of the content currently being stored nice and clean/up-to-date/dynamic/etc. However, this is a lot more work to setup once, then later it would save you time in reduced.
A sync adapter is best when the data to be downloaded is predictable, either based on user's account or temporally (such as someone having an account to download data from (email account, etc.) or when the target is fairly constant, but the data should be updated every hour or so(such as the current weather)). So this will depend a lot on your application's exact specifics.
Here is an assignment for an Android App Development course I wrote that is an even more simplified version of the first option (it has intent service + broadcast receiver for returning download results back to the Activity). Obviously since this is an assignment it has sections cut out to make skeleton code, but the documentation is ABSURDLY detailed and should be fairly easily implemented.
https://gitlab.com/vandy-aad-3/aad-3-assg-2/tree/master
Here is the extension of that assignment for that same course that focuses on implementing a simple content provider's 4 main methods (Create, Read, Update, & Delete). Everything else about the provider is given to you.
https://gitlab.com/vandy-aad-3/aad-3-assg-3/tree/master
Obviously the content being downloaded in both applications is probably not what you intended, but that is a simple swap to replace in what you do want.
Not to shill to hard, but here is the (free) Specialization that this course is a part of: https://www.coursera.org/learn/androidapps
Point one : Don't download video file within the Activity level. You can start a Service to handle it. Once the download function is finished you can start the second Activity. While download function is in progress you can show a ProgressBar
Point Two : Best Design is show a ProgressBar with percentage to user. Or disable the function. After download complete enable or start the second activity.
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.
I have read a lot of SO answers asking a similar question, but I believe my question is different. I have to load around 70-80K records from the cloud and display it to the user in a ListView. A few things that I want to be done :
I don't want to use a load more Button or load more objects when the user scrolls as I have a index from A-Z so the user could start my application and click on Z and the data should be present/available to him.
I have considered pre fetching the data using a splash screen but I was wondering if there is really any other optimised method to fetch such huge data.
I don't want to hang up the UI thread with the Loading progress bar.
I agree that this may be too much to ask for but I am just trying to see if someone has a very efficient way of doing this. I am open to ideas involving modifications in the backend on web service as well, so if you have an efficient way of achieving this using some modifications on the web service, that is also fine for me.
If it helps, look at your default contacts app, it has all the data ready and available to you when you open it. You can directly use the index to navigate to Z section. Just assume the same with 70k entries in the app.
Thanks in advance!
Here is the best solutions I figured out with the help of Tamal Mukherjee and Roman Nurik.
Tamal's solution :
Just load 5-10 rows/letter. Add a 'more' button or handle scroll event and load dynamically. Use SQLite to store data. Use background threads to fill up the db.
Roman Nurik's soltuion :
With 80k rows, that's well over 1000 items per letter in the alphabet. Seems like you'll need a lot more than letter indexing to make this UI usable. Why not offer filter-as-you-type? That'll result in more HTTP requests but might result in a better UX.
So i guess my implementation will be a combination of the both.
Please follow this step:-
1- Call API on splashscreen using IntentService.
2- Use static broadCasting and save API response into sqlitedb using ORMLite in onRecieve() method of BroadCastReciever.
3- Make sure there should be separate class for receiver.
4- Use Loader Manager for updating ListView.
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.
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.