Warm greetings.
I am quite new to android currently & had fallen under a fix. The concern is i am running the app which performs multipart upload to a remote uri using HttpPost, if the upload fails, the data is then saved to sqllite.
Now as soon as the application starts my app firstly queries the db & if there is any unsent data it first send it then proceed ahead.
What i am looking for is a feature via which i can run this task in background, i can not place this peice of code in onPause because i have few other intents which give a call to Camera, image viewer, gps etc... Any suggestions how can i manage this & how can i acheive this. Is there any samples/links available for implementation like this.?
PS:- I have the application with one single Activity defined in Manifest, other are the inbuilt Broadcasters like Camera Intent, Media Player etc. Just an additional info
Any help will be highly appreciated.
It sounds like you need a Service: http://marakana.com/forums/android/examples/60.html
Please check below link for executing the task in bg.
http://developer.android.com/reference/android/os/AsyncTask.html
Related
I am posting images using JSON. Whenever I take a picture i have to call the webservice.
This webservice call may take some time. I dont want to use Thread nor Asynchronous Task for this.
I want to call the Webservice in background and able to perform operations in foreground(button click, entering some data in edittext etc).
I guess this can be achieved by using Service. Please provide me some tutorials for this.
Is there any way?
Thanks in advance.
Services are basically used when we want to run some code in background even when a application or the present activity gets closed. Use Services only if you need it and be sure to destroy it when you are done otherwise you will waste memory and that is not good.
Here are some great tutorials of implementing service. Firstly make some sample codes to learn how to handle services and see their lifecycle, etc. and then try to implement them in you code otherwise you may be confused xamarin.com vogella.com and technotopia.com. Happy Coding!!!
If you need to handle upload within a serial queue service, take a look at IntentService.
and implement the abstracted onHandleIntent() method.
I was wondering how about the different ways in which an app can be opened, some tasks performed on the main thread and then be finished. And which way would be the best for this purpose.
I thought using an asynctask and putting the thread to sleep for sometime should be helpful and in postexecute perform the task that i want. But I am pretty sure this is a very expensive solution.
For example, If I want the app to click a photo on its own when the app is opened. The app needs to be opened and then wait so that the camera resources can be loaded properly. Once this is done the photo is clicked and once the photo is saved the app will finish.
Please provide some links if you can so I can read up and understand these concept better.
What are the better ways to go about solving this problem than using thread.sleep()..
Please share any good methods..
Thank you.
if I understand what you need (I'm not sure) you can use broadcast to cominicate between tasks
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.
Now I am working on a ListView which retrieves data from a server, and each item contains images that needs to be renderred to an ImageView. According to my knowledge of Android, the network service should be bind/unbind for each network access, and the image to be renderred should be retained from an InputStream which comes from the network service.
Briefly, the scenario is like the android market app, which renders each app with their own logos asynchronically. How does the Market App make it?
I am now totally lost how to structure the code...
Would anybody kindly help to show some snippet that works?
Look into Asynchtask of android api.
Hope this help you
How do I download multiple files in a queue one by one! I'm using this as a sample code, since.
I would be passing the URLs to download in Strings from my local DB dynamically.
Please let me know how to do that. I want the download to start as soon as the application launches. Kindly help me out!
Android Dev Type: Newbie
Purpose of Download Queue: To download multiple files from the server after in-app billing gets successful!
P.S.: I already referenced this question. But I'm not sure if that would solve my issue!
A good way of queuing up requests to be handled asynchronously, one at a time, is with an IntentService. If you have an IntentService which reads URLs from the supplied Intent, then all you have to do is create an Intent for each file you want to download, and send each Intent to the service,
Here is a good tutorial.
EDIT: I see you've already referred to a similar question, where the answer recommends IntentService. So, maybe you should use an IntentService. :)
From API 11 up, a good approach is to use a FixedThreadPool with async tasks. Do once:
ExecutorService threadPoolExecutor = Executors.newFixedThreadPool(3);
Where 3 is the number of downloads you want to run at the same time. It will queueu the task if there are already 3 downloads running, and automatically handle the task later.
Launch your async tasks with:
yourAsynTask.executeOnExecutor(threadPoolExecutor, params);
Params is probably the url you wish to connect to. You can read it out in the onPostExecute of your asynctask, and connect to the server using a HttpURLConnection.
Make sure you call down this on shutdown:
threadPoolExecutor.shutdown()
What's the best way to implement a download queue in Android?
Use an IntentService. It supplies the queue and the background thread for you, so all you have to do is put your download logic in onHandleIntent(). See here for a sample project demonstrating this.