如何从Android中的执行函数中获取返回值?

huangapple go评论80阅读模式
英文:

How do I get return values from execute functions in android?

问题

在Android中,是否可以从在execute中运行的函数中获取返回值?

调用文件:

Connector connector = new Connector();
connector.execute("login", ipPop.getText().toString(), username.getText().toString(), password.getText().toString());

函数文件:

public class Connector extends AsyncTask<String, Void, Void> {
    @Override
    protected Void doInBackground(String... voids) {
        return false;
    }
}
英文:

In android, is it possible to get return values from functions that are run in execute?

Calling file

Connector connector  = new Connector();
connector.execute(&quot;login&quot;, ipPop.getText().toString(), username.getText().toString(), password.getText().toString());

Function file

public class Connector extends AsyncTask&lt;String,Void,Void&gt;{
    @Override
    protected Void doInBackground(String... voids) {
        return false;
    }
}

答案1

得分: 1

你正在使用 Android 中的 AsyncTask。当你的类扩展它时,你可以覆盖另外三个方法。在这里,你可以覆盖 onPostExecute 方法,该方法接收来自 doInBackground 返回的数据。由于 onPostExecute 在 UI 线程上运行,你可以在那里更新用户界面,例如取消进度条。

@Override
protected void onPostExecute(String result) {
   // "result" 是从 doInBackground 方法返回的数据
   // 执行耗时操作的结果
}

参考此示例以便理解

根据最新的 Android 版本,AsyncTask 已被弃用,建议避免使用它。查看替代方案

英文:

You are using AsyncTask in Android. When your class extends it, you can override 3 more methods. Here you can override onPostExecute which is passed with data from doInBackground return. Since onPostExecute runs on UI thread you can update UI from there like cancelling progressbar.

@Override
protected void onPostExecute(String result) {
   // &quot;result&quot; is data return from doInBackground method
   // execution of result of Long time consuming operation
       
}

Refer this sample for your understanding

As per latest android version, AsyncTask is deprecated avoid using it. Check alternatives

huangapple
  • 本文由 发表于 2020年10月25日 17:19:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/64522113.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定