有没有办法从android中的Asynctask返回多个值?我有一个返回位图的任务,但我正在运行opencv,我有两个整数,我需要更新到GUI和x和y位置。
有没有办法将位图和两个整数发送到onPostExecute()方法?
谢谢!
答案 0 :(得分:0)
您可以为任务设置私有成员变量,并在doInBackground期间将数据加载到其中,并在onPostExecute期间读取它:
private class myTask extends AsyncTask<Void, Void, Boolean> {
private int xLoc = 0;
private int yLoc = 0;
@Override
protected void onPreExecute() {
// do some pre-processing, if you want
}
@Override
protected Boolean doInBackground(Void...params) {
// do something to get the new x,y values
xLoc = 100;
yLoc = 200;
return true;
}
@Override
protected void onPostExecute(final Boolean success) {
// do something with the new x, y values
Toast.makeText(getApplicationContext(),
"x = " + xLoc + ", y = " + yLoc, Toast.LENGTH_SHORT).show();
}
}