Day*_*man 127 android android-asynctask
在我的Activity中,我使用了一个从AsyncTask扩展的类和一个参数,该参数是AsyncTask的一个实例.我打电话的时候mInstanceOfAT.execute("")
一切都很好.但是当我按下再次调用AsyncTask的更新按钮时应用程序崩溃(如果网络作业不起作用).因此出现了一个例外情况
无法执行任务:任务已经执行(任务只能执行一次)
我试过为Asyctask的实例调用cancel(true),但它也不起作用.到目前为止唯一的解决方案是创建Asyntask的新实例.这是正确的方法吗?
谢谢.
Ste*_*ice 217
AsyncTask
实例只能使用一次.
相反,只需将您的任务称为 new MyAsyncTask().execute("");
来自AsyncTask API文档:
此类必须遵循一些线程规则才能正常工作:
sea*_*ges 28
在Steve Prentice的回答中详细说明了ASyncTask发生故障的实例 - 但是,当您执行ASyncTask的次数受到限制时,您可以在线程运行时自由地执行您喜欢的操作. .
将可执行代码放在doInBackground()中的循环中,并使用并发锁来触发每次执行.您可以使用publishProgress()/ onProgressUpdate()检索结果.
例:
class GetDataFromServerTask extends AsyncTask<Input, Result, Void> {
private final ReentrantLock lock = new ReentrantLock();
private final Condition tryAgain = lock.newCondition();
private volatile boolean finished = false;
@Override
protected Void doInBackground(Input... params) {
lock.lockInterruptibly();
do {
// This is the bulk of our task, request the data, and put in "result"
Result result = ....
// Return it to the activity thread using publishProgress()
publishProgress(result);
// At the end, we acquire a lock that will delay
// the next execution until runAgain() is called..
tryAgain.await();
} while(!finished);
lock.unlock();
}
@Override
protected void onProgressUpdate(Result... result)
{
// Treat this like onPostExecute(), do something with result
// This is an example...
if (result != whatWeWant && userWantsToTryAgain()) {
runAgain();
}
}
public void runAgain() {
// Call this to request data from the server again
tryAgain.signal();
}
public void terminateTask() {
// The task will only finish when we call this method
finished = true;
lock.unlock();
}
@Override
protected void onCancelled() {
// Make sure we clean up if the task is killed
terminateTask();
}
}
Run Code Online (Sandbox Code Playgroud)
当然,这比传统的ASyncTask使用稍微复杂一点,并且您放弃使用publishProgress()进行实际的进度报告.但是如果你担心内存,那么这种方法将确保在运行时只有一个ASyncTask保留在堆中.
归档时间: |
|
查看次数: |
92799 次 |
最近记录: |