如何从 AsyncTask 返回布尔值?
Posted
技术标签:
【中文标题】如何从 AsyncTask 返回布尔值?【英文标题】:How do I return a boolean from AsyncTask? 【发布时间】:2013-05-21 01:33:00 【问题描述】:我有一些用户输入 ftp 地址、用户名、密码、端口和 testConnection 按钮的 EditTexts。如果连接成功建立,则返回布尔值 true。
boolean status = ftpConnect(_address, _username, _password,_port);
ftpDisconnect();
if (status == true)
Toast.makeText(SiteManager.this, "Connection Succesful",
Toast.LENGTH_LONG).show();
else
Toast.makeText(SiteManager.this,
"Connection Failed:" + status, Toast.LENGTH_LONG).show();
我正在修改我的代码以使用 AsyncTasks 来执行各种 ftp 操作,但是如果成功建立连接,我如何传回布尔值?
testConnection.setOnClickListener(new OnClickListener()
@Override
public void onClick(View v)
_name = etSitename.getText().toString();
_address = etAddress.getText().toString();
_username = etUsername.getText().toString();
_password = etPassword.getText().toString();
_port = Integer.parseInt(etPort.getText().toString());
AsyncConnectTask task = new AsyncConnectTask(SiteManager.this,
_address, _username, _password, _port);
task.execute();
// boolean status = ftpConnect(_address, _username, _password,
// _port);
// ftpDisconnect();
// if (status == true)
// Toast.makeText(SiteManager.this, "Connection Succesful",
// Toast.LENGTH_LONG).show();
// savesite.setVisibility(0);
// else
// Toast.makeText(SiteManager.this,
// "Connection Failed:" + status, Toast.LENGTH_LONG)
// .show();
//
);
还有我的异步任务
public class AsyncConnectTask extends AsyncTask<Void, Void, Void>
private Context mContext;
private FTPHelper ftpHelper = new FTPHelper();
private String _address;
private String _user;
private String _pass;
private int _port;
ProgressDialog progressDialog;
public AsyncConnectTask(Context context, String address, String user,
String pass, int port)
mContext = context;
_address = address;
_user = user;
_pass = pass;
_port = port;
// declare other objects as per your need
@Override
protected void onPreExecute()
progressDialog = ProgressDialog.show(mContext, "Please wait for ",
"Process Description Text", true);
// do initialization of required objects objects here
;
@Override
protected Void doInBackground(Void... params)
boolean status = ftpHelper.ftpConnect(_address, _user, _pass, _port);
return null;
@Override
protected void onPostExecute(Void result)
super.onPostExecute(result);
progressDialog.dismiss();
;
【问题讨论】:
您希望状态值返回 UI 线程吗? 是的。这可能吗? 【参考方案1】:public interface MyInterface
public void myMethod(boolean result);
public class AsyncConnectTask extends AsyncTask<Void, Void, Boolean>
private MyInterface mListener;
public AsyncConnectTask(Context context, String address, String user,
String pass, int port, MyInterface mListener)
mContext = context;
_address = address;
_user = user;
_pass = pass;
_port = port;
this.mListener = mListener;
@Override
protected Boolean doInBackground(Void... params)
....
return result;
@Override
protected void onPostExecute(Boolean result)
if (mListener != null)
mListener.myMethod(result);
AsyncConnectTask task = new AsyncConnectTask(SiteManager.this,
_address, _username, _password, _port, new MyInterface()
@Override
public void myMethod(boolean result)
if (result == true)
Toast.makeText(SiteManager.this, "Connection Succesful",
Toast.LENGTH_LONG).show();
else
Toast.makeText(SiteManager.this, "Connection Failed:" + status, Toast.LENGTH_LONG).show();
);
task.execute();
如果您从 onPostExecute
调用 myMethod
,则其中的代码将在 UI 线程上运行。否则你需要通过Handler
发布一个Runnable
【讨论】:
我最终使用了您的答案,因为我真的很喜欢这种方法。 我更喜欢实现delagete模式,不依赖上下文 @Blackbelt 如果我正在传输数组,而不是布尔值`myMethod(ArrayLisy 结果)`,我将如何检查它? @Moudiz 以同样的方式。只需将boolean
更改为您要传递的对象的类型。例如ArrayList<String> results
或 byte[] result
【参考方案2】:
public class AsyncConnectTask extends AsyncTask<Void, Void, Boolean>
@Override
protected Boolean doInBackground(Void... params)
....
return true; /* or false */
@Override
protected void onPostExecute(Boolean result)
// result holds what you return from doInBackground
【讨论】:
【参考方案3】:像这样声明你的异步任务
public class AsyncConnectTask extends AsyncTask<Void, Void, Boolean>
第三个参数是doinbackground返回的结果参数。 (第一个是 asynctask 参数,第二个是进度参数)
所以你在后台和 onpostexecute 中的操作将是
@Override
protected Boolean doInBackground(Void... params)
boolean status = ftpHelper.ftpConnect(_address, _user, _pass, _port);
return status;
@Override
protected void onPostExecute(Boolean result)
// use the result
super.onPostExecute(result);
progressDialog.dismiss();
;
请记住,doInBackground 返回的值是由 onPostExecute 作为参数的。所以在 onPostExecute 方法中使用它。您也可以在此方法中更新您的 UI。
【讨论】:
谢谢!必须再等 3 分钟才能接受这个答案。谢谢解释以上是关于如何从 AsyncTask 返回布尔值?的主要内容,如果未能解决你的问题,请参考以下文章