2

Android のサーバーから 10 秒ごとに何かを送受信する必要があります。ここでは、StackOverflow とドキュメントで、それを実装するための文字通り数十の方法を見つけました (そして、私が試したすべての方法でうまくいきました) が、誰かがその方法に何か問題があると言っているようです。

AsyncTaskキャンセルされるまで(つまり、アクティビティが強制終了されるまで)ループしてみましたが、それは良い解決策ではないことがわかりました。その前に通常Threadsの で試してみたところ、バッテリーの消耗が激しいことがわかりました。

ここで提案されているコードと同様に Runnable、ScheduledExecutorService の関数でそれを実行しました: How to run a async task for every x mins in android? . 言うまでもなく、それは機能します。しかし、アクティビティがバックグラウンドにある場合、たとえばユーザーが着信に応答している場合は機能しますか?scheduleAtFixedRate

結局のところ、Android フォンでこれを行う最も適切な方法は何なのか、もうわかりません。

事前にTnx。

4

4 に答える 4

1

使用Handler方法postDelayed(Runnable r, long delayMillis),postAtTime(Runnable r, Object token, long uptimeMillis)などいろいろあります。

Handler mHandler =  new Handler() {
    public void handleMessage(Message msg) {

      //Start runnable here.
    }
   };

mHandler.sendMessageDelayed(msg, delayMillis)
于 2012-08-28T12:19:29.987 に答える
1

アプリが現在実行されていない場合でもスケジュールされたタスクを実行する必要がある場合は、 AlarmManagerBroadcastReceiversを使用することをお勧めします。

それ以外の場合、ドキュメントではハンドラーの使用を推奨しています。

AmitDの質問のコメントで議論されているように。タスクをバックグラウンドで実行する必要がある場合は、ハンドラーコールバック内でAsyncTaskを使用してこれを実現できます。

final Handler handler = new Handler() {
  public void handlerMessage(Message msg) {
    new AsyncTask<TaskParameterType,Integer,TaskResultType>() {
      protected TaskResultType doInBackground(TaskParameterType... taskParameters) {
        // Perform background task - runs asynchronously
      }
      protected void onProgressUpdate(Integer... progress) {
        // update the UI with progress (in response to call to publishProgress())
        // - runs on UI thread
      }
      protected void onPostExecute(TaskResultType result) {
        // update the UI with completion - runs on UI thread
      }
    }.execute(taskParameterObject);
  }
};
handler.postMessageDelayed(msg, delayMillis);

繰り返し実行する場合、ドキュメントにはオプションとしてScheduledThreadPoolExecutorも記載されています(これは、主に柔軟性が高いため、java.util.Timerよりも優先されるようです)。

final Handler handler = new Handler() {
  public void handlerMessage(Message msg) {
    // update the UI - runs on the UI thread
  }
};
ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor(1);
ScheduledFuture f = exec.scheduleWithFixedDelay(new Runnable() {
    public void run() {
      // perform background task - runs on a background thread
      handler.sendMessage(msg); // trigger UI update
    }
  }, 0, 10, TimeUnit.SECONDS);
于 2012-08-28T12:23:11.547 に答える
1

繰り返しタスクの実装は、主にタスクの巨大さ/処理の関数です。あなたのタスクでUIがブロックされますか。その場合、Handler と TimerTask を使用して AsyncTask を実行できます。

アクティビティ onCreate または任意の汎用関数で:

final Handler handler = new Handler();
    timer = new Timer();
        doAsynchronousTask = new TimerTask() {
           @Override
           public void run() {
            // TODO Auto-generated method stub
            handler.post(new Runnable() {
                 public void run() {
                try {
                 // Instantiate the AsyncTask here and call the execute method

                } catch (Exception e) {
                    e.printStackTrace();
                }

          }
        });
    timer.schedule(doAsynchronousTask,0,UPDATE_FREQUENCY)
    }
于 2012-08-28T12:28:52.183 に答える
0

私はTimerクラスを次のように使用すると思いますTimerTask: http://developer.android.com/reference/java/util/Timer.html

于 2012-08-28T12:17:25.157 に答える