我的Android应用程序连接到我的网站以检索和上传信息,因此我使用AsyncTask线程.
在一个实例中,我需要我的线程将true或false值返回给我的主线程.
有没有办法从AsyncTask执行函数获取此返回值?
当我执行以下操作时:
Toast.makeText(Locate.this, "Testing : "+locationUpdate.execute(location), Toast.LENGTH_LONG).show();
Run Code Online (Sandbox Code Playgroud)
我只是得到了很多胡言乱语.
我认为我需要的是暂停主线程直到第二个线程完成的方法.第二个线程调用主线程中的函数来设置我的返回值.所以当第二个线程完成时,主线程可以取消暂停并访问第二个线程设置的返回值. 如果这个逻辑是合理的,请提供建议......谢谢!
我创建了一个异步任务来调用我的服务器从DB获取数据.
我需要处理从http服务器调用返回的结果.
从我的活动我在许多地方调用异步任务.所以我不能使用成员变量来访问结果.有什么办法吗?
public Result CallServer(String params)
{
try
{
new MainAynscTask().execute(params);
}
catch(Exception ex)
{
ex.printStackTrace();
}
return aResultM;//Need to get back the result
}
private class MainAynscTask extends AsyncTask<String, Void, Result> {
@Override
protected Result doInBackground(String... ParamsP) {
//calling server codes
return aResultL;
}
@Override
protected void onPostExecute(Result result) {
super.onPostExecute(result);
//how i will pass this result where i called this task?
}
Run Code Online (Sandbox Code Playgroud)