android multiconnection downloading file like idm - android

I'm making a project like IDM, but in android I want to add multi connection to be able to download faster, but I have no idea from where should I start?

Android has feature called service. In service you can maintain task queue, which can be helpful to start with. Please check the following tutorial, which may help link

You should learn Service specifically for your requirement you should try IntentService.

You can use thread pools for your situation.You can find a starting note hereAndroid multiple thread pool and here Running code on thread pool

Related

Android, IntentService vs Custom service with HandlerThread

I have read one post about Android services within threads but there is one thing that I did not understand. In the post the writer uses a custom service because it allows multitasking while IntentService does not.
https://guides.codepath.com/android/managing-threads-and-custom-services#custom-services
Until there everything is okay, but later the writer uses a HandlerThread which just allow one thread, as my point of view there is no difference between this and a normal IntentService.
https://guides.codepath.com/android/managing-threads-and-custom-services#threading-within-the-service
Am I right? or is there anything that I am missing? I am looking at this due I want to create a android service able to run different tasks at the same time, should I use ThreadPoolExecutor instead HandlerThread?
You should make yourself familiar with the changes to background execution introduced with Android 8.0 - you cannot freely execute background work in Services anymore the way you could when that tutorial was written.
https://developer.android.com/reference/android/support/v4/app/JobIntentService might be for you; if not, have a look at https://developer.android.com/topic/libraries/architecture/workmanager

Asynchronous requests to server posted in a loop - Android

I am a Java developer with no Android experience, and I am trying to quickly put an app together. It seems that what I would normally do in Java isn't helping.
At this stage, ease of implementation is more important than efficiency or style - I will sort the latter out when there is more time and I will have educated myself properly when it comes to Android.
People can use the app to ask for support, or offer it to those who need it. Asking for support posts a request with the details to the server, and that's done.
Now I would like the app to post an asynchronous request to the server, to be notified of outstanding support requests once a minute. I guess it's the same principle of WhatsApp checking if there is any new message on the server.
I tried doing that in a separate thread with an infinite loop which sleeps for 60 seconds but for some reasons that stops the UI from working.
From what I now understand, I should use a service with a Looper, a Timer and a Handler. Is that correct?
Could anybody point me to a tutorial which explains exactly what to do, step by step? Or at least suggest keywords I should look for?
All I found so far are snippets of code which don't work together when I try to assemble it. Possibly because I am not searching for the right terms?
Thanks, Dan
You could try the following approach:
Create a service that runs in the background to check for newly added data in the server.
If you prefer to make it user-driven, you can let users refresh the list on the device to actually trigger the requests to the server.
Libraries like Retrofit can make your life easier when it comes to making http requests - always avoid the main UI thread when doing this.
Another library that you could use to decouple your application using Events is EventBus. Assuming you are running a background service to check for updates, you can use EventBus to update your User Interfaces when something new is retrieved from the server through a GET request.
I hope this gives you an idea on how to proceed with the solution. Good luck!

Download files in queue in Android

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.

Android Thread & ASynTask

I want to implement the thread in my application
first requirement is:
Each activity access the local database that time it take some time to load.So I am planning to give progress-dialog.I want to do it in thread.Currently I did using the AsynsTask because of I dodnt know how long will take for record. Other than AsynsTask How we can implement using Thread?
Multitasking Facility :
I want to run two activity.One is in background.I.E If there are any upload available(Database Syn Android to SQL Server) while running activity ,Upload should start in background.How we can implement this?
Please guide me on this
Thanks in advance
You want to synchronize your database in background so I think you have to use service in which you have to implement thread and you have to write your code in thread.
You can then schedule your service startup time and also you can repeat your service when ever you want to keep duration for start service.
And for upload you have to options
1) Using AysncTask
2) Using Service with thread
And also know that Service is running in main UI so if you want to use service for synchronize database you have to implement thread
you can execute many AsyncTasks in the background, but only one Activity can be active at a single time.
AsyncTask handles the threading for you.
In your case, AysncTask has one benefit, that when multi-user (other word, multi connection) connect to database, AsyncTask will do in serial. (But you should notice, this might change through android version, for example: at Donut, they will do together, but in Honeycomb to now, serially)

Download queue in Android

What's the best way to implement a download queue in Android?
I suspect there might be some platform classes that might do most of the work.
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.
I would suggest looking at the java.util.concurrent package and more specifically read up on Executors
You can create an ExecutorService which would only run 'n' number of Runnable objects at a time and would automatically queue up the rest of the tasks. Once one of the threads being executed finishes execution it picks up the next Runnable object in queue for execution.
Using an IntentService will make it quite difficult to support cancellation. It's just something you need to be aware of. If you can, it's API Level 9, you will be better of using http://developer.android.com/reference/android/app/DownloadManager.html
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()

Categories

Resources