I am trying to upload multiple files using IntentService. IntentService brings them to zero, as expected, the only problem is that when the Internet is down, the intent service will not stop the dongrunts, but will get stuck in the current stream. If I manage to stop the current thread, it will continue to work with other threads stored in the queue, even if the Internet connection does not work.
Another post suggested using LinkedBlockingQueue and creating your own Worker thread that constantly checks this queue for new threads. Now I know that there is some increased overhead and therefore performance problems when creating and destroying threads, but this is not a problem in my case.
At this point, all I want to do is understand how the IntentService works, which is not there yet (and I looked at the code), and then came up with my own implementation using the LinkedBlockingQueue, controlled by the Worker thread. Has anyone done this before? Could provide a working example, if you feel uncomfortable providing the source code, the pseudocode is right for me. Thanks!
UPDATE: I ended up implementing my own Intent Service using a thread that has a looper that checks the queue, which in turn saves the intent passed from startService (intent).
public class MyIntentService extends Service { private BlockingQueue<Download> queue = new LinkedBlockingQueue<Download>(); public MyIntentService(){ super(); } @Override public void onCreate() { super.onCreate(); new Thread(queueController).start(); Log.e("onCreate","onCreate is running again"); } boolean killed = false; Runnable queueController = new Runnable() { public void run() { while (true) { try { Download d =queue.take(); if (killed) { break; } else { d.downloadFile(); Log.e("QueueInfo","queue size: " + queue.size()); } } catch (InterruptedException e) { break; } } Log.e("queueController", "queueController has finished processing"); Log.e("QueueInfo","queue size: " + queue.toString()); } }; class Download { String name;
I am not sure about START_NOT_STICKY in the onStartCommand () method. If this is the correct flag to return or not. Any clarifications on this subject would be appreciated!
bytebiscuit
source share