如何从 AsyncTask 返回布尔值?

2022-09-02 00:09:53

我有一些编辑文本,用户输入ftp地址,用户名,密码,端口和testConnection按钮。如果成功建立连接,它将返回布尔值 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();
    };
}

答案 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();

如果从内部代码调用,它将在 UI 线程上运行。否则,您需要通过myMethodonPostExecuteHandler


答案 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
    }
}

推荐