将 AsyncTask 的结果返回到其实例化变量
我已经构建了一个完整工作的 AsyncTask 来处理设备授权,但唯一的问题是我无法将生成的 Bundle
返回到其实例化变量以继续在主活动中进行处理。这是 AsyncTask 实例化:
Bundle taskResult = new AuthorizeDevice(this).execute(passToAuthroization).get();
我阅读了一些有关该主题的文献,发现 onPostExecute()
方法可能与此有关,因此考虑到这一点:
protected void onPostExecute(Bundle result){
dialog.dismiss();
Toast.makeText(context, "background process finished: "+result.getString("UDID"), Toast.LENGTH_SHORT).show();
}
那么如何做我将该捆绑包带回主要活动吗?
I've constructed a fully work AsyncTask to handle device authorization, but the only catch is I'm having troubling getting the resulting Bundle
back to its instantiation variable to continue processing in the main activity. Here is the AsyncTask instantiation:
Bundle taskResult = new AuthorizeDevice(this).execute(passToAuthroization).get();
I've read some literature on the subject and found that the onPostExecute()
method may have something to do with that, so with that in mind here it is:
protected void onPostExecute(Bundle result){
dialog.dismiss();
Toast.makeText(context, "background process finished: "+result.getString("UDID"), Toast.LENGTH_SHORT).show();
}
So how do I get that bundle back to the main activity?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
AsyncTask 是非阻塞调用,您无法按照您想要的方式获取捆绑包。然而,您可以做的是在 AsyncTask 中创建一个接口,它会告诉您何时完成。像 onTaskComplete 这样的东西,你可以将生成的包传递到那里。
AsyncTask is a non-blocking call and you cannot get the bundle the way you want. However what you can do is to create an interface in AsyncTask which will tell you when it is done. Something like onTaskComplete and you can pass the resulting bundle there.
定义一个回调接口,如
实现此接口,然后对包执行任何您想要的操作。
AsyncTask
将其实现引用发送到onPostexecute() 中的
Have a callback interface defined like
Implement this interface and do whatever you want with the bundle.
Send its implementations reference to the AsyncTask
In onPostexecute() do
首先,仔细看看 API。
我需要查看更多代码才能知道您要做什么,但 PravinCG 是正确的,您使用
AsyncTask.execute()
调用的方式不正确。如果您想要返回值,请从onPostExecute()
获取它看起来您已经开始子类化
AsyncTask
这是去吧,如果您需要访问您的 Activity,您不能简单地将 AsyncTask 子类放在您的 Activity 类中吗?这将允许您从 onPostExecute() 内访问 Activity 的方法,根据设计,该方法发生在 UI 线程上。 (同样,需要查看更多代码才能知道这有多可行,只需将其放在那里即可)。如果没有,只需将对 Activity 的引用传递到 AsyncTask 子类中,并使用公共方法从 onPostExecute 返回 Bundle,或者按照建议使用接口...
First, look closer at the API.
I would need to see more of your code to know what you're trying to do, but PravinCG is correct, the way you are using the
AsyncTask.execute()
call is incorrect. If you want the return value, get it fromonPostExecute()
It looks like you've started with subclassing
AsyncTask<?,?,?>
which is the way to go, if you need access to your Activity, could you not simply place the AsyncTask subclass in your Activity class? This would allow you to access your Activity's methods from within onPostExecute(), which occurs, by design, on the UI Thread. (Again, would need to see more code to know how feasible this is, just putting it out there).if not, simply pass a reference to your Activity into your AsyncTask Subclass and use a public method to return the Bundle from onPostExecute, or use an Interface as has been suggested...
正如您所看到的,
onPostExecute()
方法的返回类型是 void。所以它不会返回任何东西。因此,您可能可以将Bundle taskResult
设为实例变量,并在 postExecute 中使用result
对其进行初始化As you can see
onPostExecute()
method's return type is void. so it does't return anything. so probably you can makeBundle taskResult
a instance variable and initialise it withresult
in postExecute