2011-11-04 26 views
7

我在嘗試使用IntentService下載多個文件。 IntentService不會按預期的方式加載它們,唯一的問題是,當互聯網關閉時,intent服務不會停止donwload,而是會停留在當前線程上。如果我設法停止當前線程,它將繼續運行存儲在其隊列中的其他線程,即使互聯網連接已關閉。使用LinkedBlockingQueue實現IntentService?

在另一篇文章中建議我使用LinkedBlockingQueue並創建自己的Worker線程,該線程會不斷地檢查此隊列中的新線程。現在我知道在創建和銷燬線程時會有一些增加的開銷和性能問題,但這對我來說不是問題。

在這一點上,我想要做的就是理解IntentService是如何工作的,至於我現在還沒有(我已經看過代碼),然後用LinkedBlockingQueue來實現它,工作者線程。有沒有人做過這個?可以提供一個工作示例,如果您提供源代碼時感到不舒服,那麼僞代碼對我來說是很好的。謝謝!

更新:我最終實現了我自己的意圖服務使用一個線程,它有一個循環檢查隊列,這反過來存儲從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; 
      } 
      public String getName(){ 
       return name; 
      } 
     } 




    public void killService(){ 
     killed = true; 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
      Download d = new Download(); 
     d.setName(intent.getStringExtra("VIDEOS")); 
     queue.add(d); 
     return START_NOT_STICKY; 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     Log.e("stopSelf","stopSelf has been just called to stop the Service"); 
     stopSelf();  
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 


} 

我不太確定onStartCommand()方法中的START_NOT_STICKY。如果這是正確的標誌返回或沒有。任何澄清,將不勝感激!

+0

我在想,而不是下載所有這些文件一個接一個,我想他們荏苒,然後下載的zip文件一次,然後當下載完成提取SD卡中的文件,這可能嗎? – bytebiscuit

回答

0

更新:我最終實現了我自己的意圖服務使用一個線程,它有一個循環檢查隊列,然後存儲從startService(intent)傳遞的意圖。

公共類MyIntentService延伸服務{

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; 
     }