2012-03-31 6 views
2

インテントサービスを使用して定期的に私のサーバにクエリを送信して、アップデートがあるかどうかを確認しています。インテントサービスには、サーバーに3秒ごとに照会するタイマータスクがあります。これは、アプリケーションが終了すると実行を開始します。
ユーザーが再びアプリケーションに戻ると、サービスを停止します。
どうすればよいですか?どのようにtimertaskをやっているインテントサービスを別のアクティビティから停止できますか?
これは私が使用しているものであるため、インテントサービスの提案をお願いします。androidでtimertaskを行っているインテントサービスを停止する

答えて

1

IntentServiceの代わりにサービスだけを使用する方がいいかもしれません。ただ、bindサービスへ

public class UpdateService extends Service { 

    public class LocalBinder extends Binder { 
     public void startUpdates() { 
      // start updateThread if it not started, or 
      // notify about resuming probes 
     } 

     public void stopUpdates() { 
      // make updateThread to wait, until startUpdates 
      // called again. 
      // 
      // REMEMBER this method can be called when startUpdates didnt called. 
     } 
    } 

    // For simplicity we will use local binder. 
    private final IBinder binder = new LocalBinder(); 

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

    private Thread updateThread = new Thread() { 
     @Override 
     public void run() { 
     while (true) { 
      // Do updates. Sleep/awake managment. 
     } 
     } 
    }; 
} 

AUTO_CREATE_FLAG付き)、あなたが必要としている時にアップデートを開始します。 アクティビティがサービスに再度バインドして更新を停止するように表示されている場合。

0

まずIntentServiceを停止することはできません。キューに存在するすべてのタスクを完了した後でのみ、IntentServiceは停止します。

関連する問題