I have 100 of images to download from server and each one size is nearly 200kb.To serve this purpose in the app,I may have to go for creating Service OR Intent service. I know that intent service is best fit in this situation because it runs on worker thread. but my only concern is, my application will also initiate other threads for different needs. so too many thread at time may leads to performance degradations. so i am in dilemma of making decision for intent service or service in separate process altogether. creating separate process might be benefitted in all the way. what do you say ?
It doesn't matter. If you remember your first multi-threading class you might be told that all threads don't work simultaneously but it is like "one runs for a while then second runs then one runs again then maybe third and so on" order is random. It doesn't affect performance because for the processor it doesn't matter if you use 2 threads or 100.
However make sure you don't wait for , say first 10, images to download first so that user don't have to wait too long for images to download o a slow connection.
Of course that Intent Service running in worker thread and Service running in Main Thread. So if you want to download images in Service you still need create new Thread so it's same work here, but Intent Service have some advantages over Service :
1.Background thread ( You no need to create Thread to download images when Service need)
2.Automatic queuing the Intents delivered to onStartCommand(), so if one Intent is being processed by onHandleIntent() on the background thread, other commands queue up waiting their turn.
3. the automatic shutdown of the IntentService when job done (in service you need to call stopService)
About create new Process it's no need. Just create one IntentService to download your images. With bigger data you can make multiple thread and download multiple part at same time but 100 images and 200kb each is really no need it.
Related
What should i use for uploading large file to the server ie service or intentservice.
I feel like Intententservice is the right approach for uploading large file because
1) It create is own worker thread,
2) No need to stop the service automatically stopped once uploaded.
If I'm going for Service we do have some plus point mentioned below:
1) We can create a thread within service for uploading large files
2) we can perform task in parallel while uploading if needed
3) If suppose Out of Memory(OUM) occurs while uploading the OS can recreate our service but for IntentService we don't have this privilege,i guess
So guys please suggests your views which one should i use for uploading large files.
Try to use Foreground service for a long running task. Because android gives more priority to foreground service than started service. Follow the below logic in android client side
Create a Service with thread implementation inside.
Make service as a foreground.
Try to set the chunk length in Http connection
Try to create the web service to support "resume upload"
To set the chunk size,
httpurlConnection.setChunkedStreamingMode(buffersize)
By setting the chunk size, we can limit the internal buffering to avoid OOM.
To get a knowledge for implementing the resume upload option, refer
this
I think the data is very large(2GB), It should be uploaded through IntentService.
Why? If you use Service then it will work in parallel no issue, but in any situation if you kill your app from background then your Service will stop immediately.
If you use IntentService then it will continue it's task until it finished, no problem if you kill the process from background. That is very important I think.
And the best Part you mention above that it works in different thread so there will be no effect on MainThread.
You should use IntentService, My Suggestion, Good Luck :)
I want to constantly, without stopping, perform 2-3 kinds of operations from my Service in Android. That is:
check if some hardware is connected and retrieve the data from it every 1 second and save it to the files
send those files a server
perform some calculations
The second job depends on the 1st one.
Note that the Service will have GUI as well if that matters, but the GUI will be used rarely. Most of them time the Service will work in "background" doing what it has to do.
How can I do that? Should there be 3 different threads or what? Or I don't need the thread because it'll be a service?
Any help is appreciated.
If you want to perform all operation in parallel in background then use android service and use ScheduledThreadPoolExecutor class to achieve this.
Otherwise use timer or Executor(with onr thread) inside android service to perform all operation in serial manner.
Let me know, This is helpful for you?
That depends on what type of service you are using, Intent Service or Service.
If you are using Intent Service, then you don't have to worry about creating a new thread, as it itself creates a worker thread.But, just keep in mind that, it takes one care of requests one at a time, in queue manner and stops itself when the processing is done.
But if you want to perform simultaneous request at once, extend from Service. You will have create a worker thread to run this service as it doesn't create a separate thread.
Check developer guide for more info:
https://developer.android.com/guide/components/services.html
To communicate between two services, you have to make use of Broadcast Receiver to receive intents which you can send from your first service or use listener callback, but i would suggest you to use Broadcast Receiver and intents.
In my application, the user can create a product and upload 5 images for it.
The user can go and create a second, a third, a forth and so on products with 5 images for each, while the first 5 images are being uploaded.
This creates a huge multithreading problem in my application.
I want the first batch of images to be uploaded before the next one starts.
At the moment Im using a regular class containing multiple AsyncTasks and callbacks and Im using some Semaphors, but I want to turn this class into a Service or an IntentService that can only be a single instance running at a time.
As I have never used Service or IntentService, I don't know which one will do the job, and then - how to implement it.
I only know that I can stop the Service whenever I want, which is good, because I already have the code that waits for all threads to stop and then terminates the whole operation, but the downfall is that multiple instances of the same Services can be started simultaneously (if im not mistaken) and the other problem is that I need to handle the threading for my service (how do I do that? Create a new thread and put all the code in a runnable to pass to the thread?)
About IntentService I know that it doesnt run on the main thread, but as soon as it starts the sequence of tasks, it dies, so this doesnt help me either, because I want to keep it alive for as long as the uploads are running.
This is all I know about Service and IntentService.
Which one should I choose and how to implement it for my needs?
Should the Service have constructors? Should I use onCreate or onStartCommand?
I'm having great difficulty understanding when to use Service vs IntentService in Android.
I'm trying to create a Manager Class that can, download, verify and install APKs.
The process of doing this require me to spawn a service(DownloadManager) to download the file, which causes my service to be destroyed prematurely.
It also needs to run an activity to install the apk.
This download manager has no front end, I just want it to be a background process that does its thing and returns the results programmatically.
I've read into both Service and Intent Service and although the documentation clearly says that Intent Services are meant to be used when the processing should be done off the UI thread, but nearly every forum I visit says that you should not do async work inside an IntentService.
For example:
Waiting for asynchronous callback in Android's IntentService
In general, an IntentService is useful for when you have discrete tasks that you want executed one at a time off of the UI thread. The IntentService will keep track of each request in a queue, execute one request at a time - on a separate, non-UI thread - and then will shut down when the queue is empty. If a new request arrives later, it will start up again, then shut down again once the queue is empty.
The warnings about running "async" work inside of an IntentService are because once onHandleIntent exits, the IntentService thinks that item has finished processing. It has no way of knowing if you created another thread that you want it to wait for. So once it has called onHandleIntent for all outstanding requests, it's going to shut down, even if there are child threads still running.
A non-intent Service gives you control over when the service starts and stops, regardless of whether there's any work to do. Also, unless you specifically make it otherwise, everything the Service does happens on the UI thread - so if you want work done on a background thread, you need to explicitly implement that. It's also up to you to implement how the Service handles multiple incoming requests. But, the service won't shut down until you tell it to (or the OS runs out of resources).
It sounds based on your description like you probably have two choices:
If you're ok with the service processing requests one at a time, you could use an IntentService - but you'll need to make onHandleIntent wait for each request to finish. This is still happening off of the UI thread, but it does mean that if you have multiple download requests, they're not going to happen in parallel.
You could use a non-intent Service to process each download request on its own child thread, all in parallel. Then it's up to you to keep track of all the processing threads.
I've developed an application and one of its aims is to upload a file to the server. On average the file is quite large and upload takes a lot of time, so to make sure it'll be processed till the end, I moved the critical part of IO to the IntentService. From the IntentService for every single upload there's an AsyncTask started which does the IO job. Unfortunately the screen scrolling gets freezed several times while doing the upload in the background. I thought that a combination of IntentService+AsyncTask should be enough...
I'd be grateful for any suggestions how to solve it..
I think you may be misusing IntentService. I am assuming that you are calling AsyncTask.execute(...) from IntentService.onHandleIntent(Intent).
IntentService was created to perform background tasks one at a time on a non-UI thread. This means that onHandleIntent(Intent) is invoked from a background thread and should not be creating AsyncTasks. Furthermore, once onHandleIntent(Intent) finishes (and it does so immediately if all you do is start an AsyncTask) the service is a candidate for being shut down.
I know that this doesn't directly answer your questions but it may point to the causes of the issue.
Note that if you need to be able to handle multiple requests concurrently then your best bet is just to extend Service and enqueue a work request onto an Executor in onStartCommand(...).
[EDIT] For more information on proper usage of IntentService check this out.