How do I return a boolean from AsyncTask?

后端 未结 3 1989
闹比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:43
    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();
    

    If you call myMethod from onPostExecute the code inside it will run on the UI Thread. Otherwise you need to post a Runnable through a Handler

    0 讨论(0)
  • 2020-11-22 07:50
    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
        }
    }
    
    0 讨论(0)
  • 2020-11-22 07:56

    Declare Your asynctask like

    public class AsyncConnectTask extends AsyncTask<Void, Void, Boolean>
    

    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.

    0 讨论(0)
提交回复
热议问题