4

私のユースケースは、ユーザーがEditTextを入力するたびに、入力データがバックグラウンドで操作を実行するために使用されることです。これらの操作は、ANRを引き起こすのに十分な時間がかかる場合があります。@TextChangeと@Backgroundは、操作が十分に迅速に行われていれば正常に機能します。ただし、操作には十分な時間がかかるため、ユーザーがより多くのデータを入力すると、同じUIコンポーネントの更新をコマンドする複数のバックグラウンドタスクが発生するため、スレッドの問題が発生します。

AsyncTask APIを使用して目的の動作を実現したと思いますが、コードが大幅に簡素化されるため、AndroidAnnotationsベースのソリューションも探したいと思いました。ちなみに素晴らしいlib。

以下は、私の主張をうまく説明するコードスニペットです。少なくとも読んでくれてありがとう、コメント/回答はありがたいです:)

@TextChange
void onUserInput(...) {
  // This will start a new thread on each text change event
  // thus leading to a situation that the thread finishing
  // last will update the ui
  // the operation time is not fixed so the last event is
  // not necessary the last thread that finished
  doOperation()
}

@Background
void doOperation() {
  // Sleep to simulate long taking operation
  Thread.sleep( 6000 );
  updateUi()
}

@UiThread
void updateUi() {
  // Update text field etc content based on operations
}

更新:これは現時点では不可能です。以下のDaySの回答を参照してください。

4

2 に答える 2

4

AA3.0以降で可能です。このスレッドを読んでください。

@Override
protected void onStop() {
    super.onStop();
    boolean mayInterruptIfRunning = true;
    BackgroundExecutor.cancelAll("longtask", mayInterruptIfRunning);
}

@Background(id="longtask")
public void doSomethingLong() {
    // ...
}
于 2014-02-02T20:30:43.933 に答える
1

Android Annotationsにはすでにこの種のリクエストがありましたが、解決策が提案されなかったため、クローズされました。しかし、それについて何か考えがあれば、先に進んでこの問題を再度開いてください;)

于 2013-03-13T11:02:15.867 に答える