2011-09-14 7 views
3

Androidでアプリケーションを作成しようとすると、3分ごとにアクティビティが更新されます。これは、アプリケーションがニュースフィードをリフレッシュするTwitterやFacebookのタイプのアプリ数分おきに自分自身で、私はどのようにこれが行われたかについてのアイデアを与えるWeb上のチュートリアルを見つけることができません、どんな助けも遠慮なく行こう!アンドロイドでオートリフレッシュメソッドを構築する

答えて

5

バックグラウンドで実行される非同期更新タスクを作成し、更新メソッドを起動するか、3分ごとにインテントでアクティビティをまっすぐに再起動する必要があります。次のようなものがあります。

private class YourUpdateTask extends AsyncTask<Integer, Void, Integer> { 
     /** 
     * The system calls this to perform work in a worker thread and delivers 
     * it the parameters given to AsyncTask.execute() 
     */ 
     protected Integer doInBackground(Integer... millis) { 
      try { 
       int waited = 0; 
       int duration = yourTimeHere; 
       while (waited < duration) { 
        Thread.sleep(100); 
        waited += 100; 
       } 
      } catch (InterruptedException e) { 
       // do nothing 
      } 

      updateState(); 

      return 1; 
     } 

     /** 
     * The system calls this to perform work in the UI thread and delivers 
     * the result from doInBackground() 
     */ 
     protected void onPostExecute(Integer result) { 
      refreshActivity(); 
     } 
    } 
+0

ここではタイマー/タイマータスクがより適切なようです –

関連する問題