Implementation of IntentService using LinkedBlockingQueue?

前端 未结 1 437
花落未央
花落未央 2021-02-09 12:12

I\'m trying to download multiple files using IntentService. The IntentService donwloads them okey as expected one at a time, the only problem is that when the Internet is down t

相关标签:
1条回答
  • 2021-02-09 13:12

    UPDATE: I eventually implemented my own Intent Service using a thread that has a looper which checks the queue which in turn stores the intents passed from the 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;
            //Download files process
            void downloadFile() { 
                   //Download code here
             }
    
                Log.e("Download","Download being processed is: " + name);
            }
            public void setName(String n){
                name = n;
            }
    
    0 讨论(0)
提交回复
热议问题