2011-11-04 24 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; 
     }