Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I return a boolean from AsyncTask?

Tags:

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

I'm reworking my code to use AsyncTasks to perform the various ftp operations, but how can I pass back a boolean value if a connection is successfully made?

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

And my AsyncTask

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();     }; } 
like image 447
RapsFan1981 Avatar asked May 25 '13 17:05

RapsFan1981


People also ask

How to return value from Async Task Android?

You can retreive the return value of protected Boolean doInBackground() by calling the get() method of AsyncTask class : AsyncTaskClassName task = new AsyncTaskClassName(); bool result = task. execute(param1,param2......).

What is AsyncTask explain it in detail?

An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. An asynchronous task is defined by 3 generic types, called Params , Progress and Result , and 4 steps, called onPreExecute , doInBackground , onProgressUpdate and onPostExecute .

How to use AsyncTask Android?

Android AsyncTask going to do background operation on background thread and update on main thread. In android we cant directly touch background thread to main thread in android development. asynctask help us to make communication between background thread to main thread.

What is Asyntask?

Android AsyncTask is an abstract class provided by Android which gives us the liberty to perform heavy tasks in the background and keep the UI thread light thus making the application more responsive. Android application runs on a single thread when launched.


2 Answers

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

like image 119
Blackbelt Avatar answered Oct 05 '22 15:10

Blackbelt


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     } } 
like image 38
Alexander Kulyakhtin Avatar answered Oct 05 '22 16:10

Alexander Kulyakhtin