Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8528437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:48:04+00:00 2026-06-11T08:48:04+00:00

I have an activity class as below. public class LoginActivity extends Activity implements OnClickListener

  • 0

I have an activity class as below.

public class LoginActivity extends Activity implements OnClickListener {
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      button1 = (ImageView) findViewById(R.id.button1);
      button1.setOnClickListener(this);
   }
   @Override
   public void onClick(View v) {
       BackgroundRunner bgRunner = new BackgroundRunner(this);
       String[] params = new String[]{url};
       bgRunner.execute(params);
   }


}

And the AsyncTask is:

 public class BackgroundRunner extends AsyncTask<String, Void, Boolean>  {

@Override
protected Boolean doInBackground(String... params) {
    ServiceCaller serviceCaller = new ServiceCaller();
    boolean status = serviceCaller.checkLogin(params[0]);
    return status;
}

@Override
public void onPreExecute(){
    progressBar = (ProgressBar) currentContext.findViewById(R.id.loader);
    progressBar.setVisibility(View.VISIBLE);
     }

@Override
public void onPostExecute(final Boolean status){
    progressBar.setVisibility(View.INVISIBLE);
}

}

Here is the scenario. The main activity class creates a thread on a click. The then created thread fetches some data from the server. It is a time consuming task. So a progress bar is displayed on the UI. Currently I am using AsyncTask to accomplish server data retrieval. But the real challenge is wait for the background task to complete and get the value from it. What I am looking for is:

wait until server calls are made and get the results. Meanwhile show the progress bar. I think Handler would be an option. I am far less clear on that.

Any thoughts?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T08:48:05+00:00Added an answer on June 11, 2026 at 8:48 am

    My solution was create my own asynctask class:

     import android.app.Activity;
    import android.app.ProgressDialog;
    import android.content.DialogInterface;
    import android.content.DialogInterface.OnCancelListener;
    import android.os.Handler;
    import br.com.zcr.ezcrm.R;
    
    public class AsyncTask implements Runnable {
    
        private Activity activity;
        private Handler handler;
        private Action action;
        private boolean showDialog = true;
        private boolean running = false;
        private boolean canceled = false;
    
        private Thread t;
    
        private ProgressDialog progress;
    
        public AsyncTask(Activity activity) {
            this.activity = activity;
            handler = new Handler();
        }
    
        public AsyncTask(Activity activity, Action action) {
            this.activity = activity;
            this.action = action;
            handler = new Handler();
        }
    
        private ProgressDialog getDialog() {
            if (progress != null)
                return progress;
            progress = ProgressDialog.show(activity, null, activity.getString(R.string.carregando), true, false);
            progress.setOnCancelListener(new OnCancelListener() {
                public void onCancel(DialogInterface dialog) {
                    setCanceled(true);
                }
            });
            return progress;
        }
    
        private void showDialog() {
            if (showDialog)
                getDialog().show();
        }
    
        private void hideDialog() {
            if (showDialog)
                getDialog().dismiss();
        }
    
        public void execute(boolean showDialog) {
    
            this.showDialog = showDialog;
            execute();
        }
    
        public void execute() {
    
            if (running || action == null)
                return;
    
            running = true;
    
            // Utils.setFixedOrientation(activity);
            showDialog();
            t = new Thread(AsyncTask.this);
            t.start();
        }
    
        @Override
        public void run() {
    
            try {
    
                final Object o = action.run();
    
                if (canceled)
                    return;
    
                handler.post(new Runnable() {
                    public void run() {
                        action.onFinnish(o);
                    }
                });
    
            } catch (final Exception e) {
    
                if (canceled)
                    return;
    
                handler.post(new Runnable() {
                    public void run() {
                        action.onError(e);
                    }
                });
    
            } finally {
    
                canceled = false;
                hideDialog();
                // Utils.setUnfixedOrientation(activity);
                running = false;
            }
    
        }
    
        /*
         * public void stop() { running = false; }
         */
        public void setAction(Action a) {
            action = a;
        }
    
        public void setCanceled(boolean canceled) {
            if (canceled)
                t = null;
            this.canceled = canceled;
        }
    
        public interface Action {
            /** Acao a ser executada */
            public Object run() throws Exception;
    
            /** Chamado no fim de todas as execucoes */
            public void onFinnish(Object result);
    
            /** Para qualquer execucao e retorna o erro */
            public void onError(Exception e);
        }
    
    }
    

    And this is the implementation:

        AsyncTask task = new AsyncTask(this, new Action() {
            public Object run() throws Exception {
                return WebService.autenticate(login, pass);
            }
            public void onFinnish(Object result) {
    //result was returned in run method
                verifyLogin((String) result);
            }
            public void onError(Exception e) {
                //error
            }
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an activity class as below. public class LoginActivity extends Activity implements OnClickListener
If I have defined a Activity: public class DialogActivity extends Activity{ @Override public void
So i have this activity : public class settings_dock extends Activity { AlertDialog alert;
I have an class as below: public class FYPSmsReceiverBroadcast extends BroadcastReceiver I need to
I have class A which extends the Activity class. This class is in package
I have a list of objects called Activity: class Activity { public Date activityDate;
I have the next code: class Printer{ Activity activity; public Printer (Activity activity) {
Actually i have created an singleton class. Now my singleton class extends Activity, and
I have a LoginActivity.java that imports VersionCheck.java. VersionCheck.java has a class VersionCheck that extends
OK, I have a login class like the snippet below public class LoginClass {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.