2011-11-04 22 views
7

IntentServiceを使用して複数のファイルをダウンロードしようとしています。 IntentServiceはそれらを一度に1つずつ期待どおりにdonwloadしますが、唯一の問題は、インターネットがダウンしているときにインテントサービスが現在のスレッドで停止するのではなく、ドンドロードを停止しないことです。現在のスレッドを停止すると、インターネット接続が切断されても、キューに格納されている他のスレッドが実行され続けます。LinkedBlockingQueueを使用したIntentServiceの実装?

私はLinkedBlockingQueueを使用し、常に新しいスレッドのためにこのキューをチェックする独自のワーカースレッドを作成することを別の記事で提案しました。今私はいくつかのオーバーヘッドが増え、スレッドを作成して破壊する際のパフォーマンスの問題があることを知っていますが、私の場合はそれが問題にはなりません。

私がしたいのは、IntentServiceがどのように動作しているのか理解できていないことです(私はコードを見てきました)。そして、LinkedBlockingQueueを使って独自の実装をワーカースレッド。誰もこれを前にしたことがありますか?あなたがソースコードを提供することに不快感を感じる場合、擬似コードは私によっては問題ありません。ありがとう!

UPDATE:私は最終的に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

サービスを拡張しますこれは可能ですか? – bytebiscuit

答えて

0

UPDATE:私は最終的に順番にSTARTSERVICE(意図)から渡されたインテントを格納するキューをチェックルーパーを持っている糸を使用して自分の意思サービスを実施しました。

パブリッククラスMyIntentServiceは、私が代わりに一つ一つが、私はそれらをビュンと思っていますすべてのこれらのファイルをダウンロードするので、思考やダウンロードがあるときに、その後、一度zipファイルをダウンロードしています{

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; 
     } 
関連する問題