gpt4 book ai didi

android - 检查工作 AsyncTask

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:50:52 27 4
gpt4 key购买 nike

我在另一个线程中有一些工作要做(通过 doingAsyncTask)。当用户点击按钮时工作开始。但同时只有一个 doingAsyncTask 对象必须完成这项工作,我认为如果 doingAsyncTask 正在工作,则单击按钮不能创建一个新的 doingAsyncTask 对象并执行它,它必须等到工作完成。我该如何检查?

public class SearchActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//....
}

public void onclickButton(View view) {
new doingAsyncTask().execute();
}

public class doingAsyncTask extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... unused) {
//doing something
return(null);
}

protected void onProgressUpdate() {
}
protected void onPreExecute() {
}
protected void onPostExecute() {
}
}
}

已解决谢谢大家,它对我有用

      if(task.getStatus() == AsyncTask.Status.FINISHED)
task=new ProgressBarShow();
if(task.getStatus() == AsyncTask.Status.PENDING){
//task=new ProgressBarShow();
task.execute();
}

最佳答案

检查这个AsyncTask.Status

AsyncTask.Status    FINISHED    Indicates that onPostExecute(Result) has finished. 
AsyncTask.Status PENDING Indicates that the task has not been executed yet.
AsyncTask.Status RUNNING Indicates that the task is running.

代码:

if (doingAsyncTask().getStatus().equals(AsyncTask.Status.FINISHED))
doingAsyncTask().execute();
else

编辑:

public class SearchActivity extends Activity {
doingAsyncTask asyncTask;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ...
asyncTask = new doingAsyncTask();
}
public void onclickButton(View view) {
if(ayncTask.getStatus().equals(AsyncTask.Status.FINISHED) || ayncTask.getStatus().equals(AsyncTask.Status.PENDING)) {
asyncTask.execute();
}
else {
// do something
}
}
// ...
}

关于android - 检查工作 AsyncTask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10042922/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com