英文:
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("login", ipPop.getText().toString(), username.getText().toString(), password.getText().toString());
Function file
public class Connector extends AsyncTask<String,Void,Void>{
@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) {
// "result" 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论