将 AsyncTask 的结果返回到其实例化变量

发布于 2024-11-29 20:22:31 字数 532 浏览 1 评论 0原文

我已经构建了一个完整工作的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情徒 2024-12-06 20:22:31

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.

枕花眠 2024-12-06 20:22:31

定义一个回调接口,如

public interface AuthorisationCallback() {
      void onFinish(Bundle bun);
};

实现此接口,然后对包执行任何您想要的操作。

AuthorisationCallback mAuthcallback = new AuthorisationCallback() {
    void onFinish(Bundle bun) {
        // Do what ever with the bundle.
    }
};

AsyncTask

new AuthorizeDevice(mAuthcallback).execute(passToAuthroization);

将其实现引用发送到onPostexecute() 中的

protected void onPostExecute(Bundle result){
    dialog.dismiss();
    mCallback.onFinish(result);
}

Have a callback interface defined like

public interface AuthorisationCallback() {
      void onFinish(Bundle bun);
};

Implement this interface and do whatever you want with the bundle.

AuthorisationCallback mAuthcallback = new AuthorisationCallback() {
    void onFinish(Bundle bun) {
        // Do what ever with the bundle.
    }
};

Send its implementations reference to the AsyncTask

new AuthorizeDevice(mAuthcallback).execute(passToAuthroization);

In onPostexecute() do

protected void onPostExecute(Bundle result){
    dialog.dismiss();
    mCallback.onFinish(result);
}
浅笑依然 2024-12-06 20:22:31

首先,仔细看看 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 from onPostExecute()

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...

七颜 2024-12-06 20:22:31

正如您所看到的,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 make Bundle taskResult a instance variable and initialise it with result in postExecute

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文