1

の次の実装がありAsyncTask、複数AsyncTaskの を同時に実行できます。

public abstract class MyAsyncTask<Params, Progress, Result> extends AsyncTask<Params, Progress, Result> {

    public AsyncTask<Params, Progress, Result> executeCompat(Params... params) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            return executeOnExecutor(THREAD_POOL_EXECUTOR, params);
        } else {
            return execute(params);
        }
    }
}

通常の混乱や誤った使用を避けるためにAsyncTask、コードから次へのアクセスをブロックしたいと思います。

  • AsyncTaskクラスのみを使用するMyAsyncTask必要があります。
  • execute()関数MyAsyncTask

これを行うことは可能ですか?

4

2 に答える 2

2

My idea of doing this would be a bit different. Instead of extending the AsyncTask class, you can create a method that takes as a parameter the AsyncTask you want to use. Here is an example:

public void executeAsyncTask(AsyncTask asyncTask) {

   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        asyncTask.executeOnExecutor(THREAD_POOL_EXECUTOR, params);
    } else {
        asyncTask.execute(params);
    }
}

With this you just need to instantiate the AsyncTask you want to use without worrying about the problems you mentioned since you will be extending the Android's AsyncTask class having overriden the execute method the way you want. So lets say that you have defined an AsyncTask named customAsyncTask, to execute it you just call executeAsyncTask(new customAsyncTask(your_params)); You can define the above method in a static class (making the method also static) for easier access.

Hope that helps:)

于 2012-07-05T22:22:44.667 に答える
0

クラスをインポートせずAsyncTask、インポートするだけです。MyAsyncTaskそうすれば、あなたのクラスが唯一の利用可能なオプションです。

メインファイルのAsyncTaskメソッドを上書きできると思います。ただし、MyAsyncTask用の新しいクラスファイルが必要です。そうしないと、正しく継承されません。

于 2012-06-27T15:46:28.900 に答える