这是我到目前为止所做的:
我的问题是: 通过实现,我使用委托来设置对调用活动的引用。我找不到改变代表团的方法。如果我想在我的第二个活动中创建一个新的asynctask并尝试委托它,它会显示一个不兼容的类型错误。
那么我有没有办法将asyncresults发送回特定的通话活动?
实施示例
public class MainActivity extends AppCompatActivity implements AsyncResponse
public void startAsync(String[] stringArray)
{
AsyncTaskRequest objMyTask = new AsyncTaskRequest(this);
// objMyTask.delegate = this; // *.delegate in another Task in not possible
objMyTask.execute(stringArray);
}
AsyncResponse接口
public interface AsyncResponse
{
void taskDone(JSONObject x);
}
AsyncTask的示例
public class AsyncTaskRequest extends android.os.AsyncTask<String,
Void, JSONObject>
{
//MainActivity delegate = null;
private AsyncResponse delegate;
public AsyncTaskRequest(AsyncResponse delegation)
{
delegate=delegation;
}
protected JSONObject doInBackground(String... postData)
{.......}
@Override
protected void onPostExecute(JSONObject x)
{
delegate.taskDone(x);
}
}
答案 0 :(得分:0)
您可以在ASyncTask中使用侦听器
在您的ASyncTask中:
public class AsyncTaskRequest extends android.os.AsyncTask<String,
Void, JSONObject>
{
private OnTaskCompleted listener;
public interface OnTaskCompleted{
void onTaskCompleted(Boolean output);
}
private AsyncResponse delegate;
public AsyncTaskRequest(AsyncResponse delegation)
{
delegate=delegation;
}
protected JSONObject doInBackground(String... postData)
{.......}
@Override
protected void onPostExecute(JSONObject x)
{
delegate.taskDone(x);
if (listener != null)
listener.onTaskCompleted(result);
}
}
并在你的java文件中调用这个函数:
public OnTaskCompleted listener =new OnTaskCompleted() {
public void onTaskCompleted(Boolean output) {
//your code here
}
};