How do I return a boolean from AsyncTask?

后端 未结 3 2040
闹比i
闹比i 2020-11-22 07:20

I have some EditTexts that a user enters an ftp address, username, password, port anda testConnection button. If a connection is successfully estabished it returns a boolean

3条回答
  •  一整个雨季
    2020-11-22 07:56

    Declare Your asynctask like

    public class AsyncConnectTask extends AsyncTask
    

    The third parameter is the result parameter that is returned by doinbackground. (The first one is asynctask param and second one is progress parameter)

    so your do in background and onpostexecute will be

    @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();
    };
    

    Remember that the value returned by doInBackground is cathced by onPostExecute as parameter. so use this in the onPostExecute method. you can update your UI in in this method also.

提交回复
热议问题