Android 使用LinkedBlockingQueue实现IntentService?

Android 使用LinkedBlockingQueue实现IntentService?,android,Android,我正在尝试使用IntentService下载多个文件。IntentService一次只加载一个线程,但唯一的问题是当Internet断开时,IntentService不会停止加载,而是会卡在当前线程上。如果我设法停止当前线程,它将继续运行其队列中存储的其他线程,即使internet连接已断开 另一篇文章建议我使用LinkedBlockingQueue并创建自己的工作线程,以不断检查此队列中的新线程。现在我知道在创建和销毁线程时会增加一些开销,从而导致性能问题,但在我的例子中,这不是一个问题 在这

我正在尝试使用IntentService下载多个文件。IntentService一次只加载一个线程,但唯一的问题是当Internet断开时,IntentService不会停止加载,而是会卡在当前线程上。如果我设法停止当前线程,它将继续运行其队列中存储的其他线程,即使internet连接已断开

另一篇文章建议我使用LinkedBlockingQueue并创建自己的工作线程,以不断检查此队列中的新线程。现在我知道在创建和销毁线程时会增加一些开销,从而导致性能问题,但在我的例子中,这不是一个问题

在这一点上,我所要做的就是理解IntentService是如何工作的,而我目前还没有(我已经看过代码),然后使用工作线程控制的LinkedBlockingQueue为它提供我自己的实现。以前有人这样做过吗?可以提供一个工作示例,如果您觉得提供源代码不舒服,我可以使用伪代码。谢谢

更新:我最终使用一个线程实现了我自己的Intent服务,该线程有一个循环器来检查队列,该队列反过来存储从startService(Intent)传递的Intent

公共类MyIntentService扩展服务{
private BlockingQueue=新建LinkedBlockingQueue();
公共MyIntentService(){
超级();
}
@凌驾
public void onCreate(){
super.onCreate();
新线程(queueController).start();
Log.e(“onCreate”,“onCreate正在再次运行”);
}
布尔值=假;
Runnable queueController=新的Runnable(){
公开募捐{
while(true){
试一试{
下载d=queue.take();
如果(被杀){
打破
}
否则{
d、 下载文件();
Log.e(“QueueInfo”,“队列大小:”+queue.size());
}
}
捕捉(中断异常e){
打破
}
}
Log.e(“queueController”,“queueController已完成处理”);
Log.e(“QueueInfo”,“队列大小:”+queue.toString());
}
};
类下载{
字符串名;
//下载文件过程
void downloadFile(){
//在这里下载代码
}
Log.e(“下载”,“正在处理的下载为:“+名称”);
}
公共void集合名(字符串n){
name=n;
}
公共字符串getName(){
返回名称;
}
}
公共服务{
死亡=真;
}
@凌驾
公共int onStartCommand(Intent Intent、int标志、int startId){
下载d=新下载();
d、 setName(intent.getStringExtra(“视频”);
添加(d);
返回开始时间不粘;
}
@凌驾
公共空间{
super.ondestory();
Log.e(“stopSelf”,“stopSelf刚刚被调用以停止服务”);
stopSelf();
}
@凌驾
公共IBinder onBind(意向){
返回null;
}
}

我不太确定onStartCommand()方法中的START\u not\u STICKY。是否返回正确的标志。如有任何澄清,将不胜感激

更新:我最终使用一个线程实现了我自己的Intent服务,该线程有一个循环器来检查队列,该队列反过来存储从startService(Intent)传递的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;
        }
private BlockingQueue=new LinkedBlockingQueue();
公共MyIntentService(){
超级();
}
@凌驾
public void onCreate(){
super.onCreate();
新线程(queueController).start();
Log.e(“onCreate”,“onCreate正在再次运行”);
}
布尔值=假;
Runnable queueController=新的Runnable(){
公开募捐{
while(true){
试一试{
下载d=queue.take();
如果(被杀){
打破
}
否则{
d、 下载文件();
Log.e(“QueueInfo”,“队列大小:”+queue.size());
}
}
捕捉(中断异常e){
打破
}
}
Log.e(“queueController”,“queueController已完成处理”);
Log.e(“QueueInfo”,“队列大小:”+queue.toString());
}
};
类下载{
字符串名;
//下载文件过程
void downloadFile(){
//在这里下载代码
}
Log.e(“下载”,“正在处理的下载为:“+名称”);
}
公共void集合名(字符串n){
name=n;
}

我在想,不是一个接一个地下载所有这些文件,而是将它们压缩,然后下载一次zip文件,然后在下载完成后将文件解压缩到sd卡中,这可能吗?
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;
        }