Help us understand the problem. What is going on with this article?

AsyncTaskにリスナーを追加してActivityで処理する

More than 5 years have passed since last update.

原型のAsyncTask

AsyncTaskSample
public class AsyncTaskSample extends AsyncTask<Void, Integer, String> {
    //Activiyへのコールバック用interface
    public interface AsyncTaskCallback {
        void preExecute();
        void postExecute(String result);
        void progressUpdate(int progress);
        void cancel();
    }

    private AsyncTaskCallback callback = null;;

    public AsyncTaskSample(AsyncTaskCallback _callback) {
        this.callback = _callback;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        callback.preExecute();
    }

    @Override
    protected void onCancelled() {
        super.onCancelled();
        callback.cancel();
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        callback.postExecute(result);
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
        callback.progressUpdate(values[0]);
    }

    @Override
    protected String doInBackground(Void... params) {
        return null;
    }
}

(空行)
実装するActivity

Activity
public class SampleActivity {
    public void onCreate(Bundle savedInstanceState) {
        AsyncTaskSample sampleTask = new AsyncTaskSample(new AsyncTaskCallback() {
            public void preExecute() {
                //だいたいの場合ダイアログの表示などを行う
            }
            public void postExecute(String result) {
                //AsyncTaskの結果を受け取ってなんかする
            }
            public void progressUpdate(int progress) {
                //だいたいプログレスダイアログを進める
            }
            public void cancel() {
                //キャンセルされた時になんかする
            }
        });
        sampleTask.execute();
    }
}

または

Activity
public class SampleActivity implements AsyncTaskCallback {
    public void onCreate(Bundle savedInstanceState) {
        AsyncTaskSample sampleTask = new AsyncTaskSample(this);
        sampleTask.execute();
    }
    public void preExecute() {
        //だいたいの場合ダイアログの表示などを行う
    }
    public void postExecute(String result) {
        //AsyncTaskの結果を受け取ってなんかする
    }
    public void progressUpdate(int progress) {
        //だいたいプログレスダイアログを進める
    }
    public void cancel() {
        //キャンセルされた時になんかする
    }
}
uzabase
企業活動の意思決定を支える情報インフラの提供
https://www.uzabase.com/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした