我正在尝试使用IntentService下载多个文件。 IntentService一次按预期的方式将它们卸载,这是唯一的问题,当Internet断开时,intent服务不会停止下载,而会卡在当前线程中。如果我设法停止当前线程,即使Internet连接断开,它也将继续运行存储在其队列中的其他线程。

在另一篇文章中建议我使用LinkedBlockingQueue并创建自己的Worker线程,该线程不断检查此队列中是否有新线程。现在,我知道在创建和销毁线程时会增加一些开销,因此会出现性能问题,但是就我而言,这不是问题。

在这一点上,我要做的就是了解IntentService的工作原理,到目前为止我还不了解(我已经看过代码),然后使用Worker线程控制的LinkedBlockingQueue提出自己的实现。有人做过吗?可以提供一个工作示例,如果您不满意提供源代码,那么伪代码对我来说很好。谢谢!

更新:我最终使用带有循环程序的线程实现了自己的 Intent 服务,该线程检查循环,该循环依次存储从startService(intent)传递的 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。如果它是正确的标志,则返回。任何澄清,将不胜感激!

最佳答案

更新:我最终使用具有循环程序的线程实现了自己的 Intent 服务,该线程检查了队列,该队列又存储了从startService(intent)传递来的 Intent 。

公共(public)类MyIntentService扩展了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;
        }

10-07 19:26
查看更多