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

  • SEARCH
  • Home
  • 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 7846737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:41:05+00:00 2026-06-02T17:41:05+00:00

I am implementing AsyncTask for my understanding of concepts. The code is working but

  • 0

I am implementing AsyncTask for my understanding of concepts. The code is working but not in way I want to. I have gone through the documents but cannot pin point what wrong am I doing.

Code:

private class NewThread extends AsyncTask<Integer, Integer , String>
    {

        @Override
        protected String doInBackground(Integer... params) {
            Log.d(TAG,"inside doInBackground");
            for (int i=0;i<params.length;i++)
            {
                 try {
                publishProgress(i);
                    Thread.sleep(6000);

                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
            return "Finished";
        }

        @Override
        protected void onPostExecute(String result) {

            super.onPostExecute(result);
            Toast.makeText(getBaseContext(), result, Toast.LENGTH_SHORT).show();
            Log.d(TAG,"inside onPostExecute");
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            // TODO Auto-generated method stub
            super.onProgressUpdate(values);
            Toast.makeText(getBaseContext(), "Done " + values[0], Toast.LENGTH_SHORT).show();
            Log.d(TAG,"inside onProgressUpdate");
        }

I only see Done 0 and Finished. What I was expecting is Done0 , Finished, Done1, Finished, Done2. Finished…. OR Done0 Done1 Done2 ….Finished.

What should I change in code to do that? Is it even possible?

I am calling AsyncTask using a button.

public void onClick(View v) {
        // TODO Auto-generated method stub
        new NewThread().execute(4);


    }

Thanks

  • 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-02T17:41:08+00:00Added an answer on June 2, 2026 at 5:41 pm

    The code you use to start the AsyncTask starts 1 (one) instance, passing 4 as a parameter. Therefore, param.length() is 1 (one) and you get only one output.

    Try this instead:

    new NewThread().execute(1,2,3,4);
    

    Or maybe this, if you want to see several threads working simultaneously:

    new NewThread().execute(1);
    new NewThread().execute(2);
    new NewThread().execute(3);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

while implementing hhtps, is there a way not to show the message Do you
implementing publishActivity in PHP using the REST API using this code: $activity = array(
Implementing a simple Login screen using JSF and Spring and Hibernate. I have written
When implementing the Strategy Pattern, where does one put the code that determines which
When implementing HTTP services in node.js, there is a lot of sample code like
I am implementing a code that based upon a particular response from a web
When implementing move constructors and move assignment operators, one often writes code like this:
Im implementing NFS and almoste done but the RFC section 3.3.8 says this in
I have a strangle behavior when implementing AsynTask and ProgressDialog. When i download small
Implementing agile in projects requires the ability to do refactoring. It is not really

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.