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 8512699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:21:14+00:00 2026-06-11T04:21:14+00:00

Asycntask’s onprogressupdate is always being called, : @Override protected void onProgressUpdate(Integer… values) { pbSubStatus.incrementProgressBy(values[1]);

  • 0

Asycntask’s onprogressupdate is always being called, :

    @Override
    protected void onProgressUpdate(Integer... values) {
        pbSubStatus.incrementProgressBy(values[1]);
        rawNum = values[0];
                     ...
    }

And on outer class:

    private double getCurrentTime() {
    return currentTime;
}
private void doSomethingOnThread() {
    LinearLayout layout = (LinearLayout) findViewById(R.id.main);
    sMeter = new Meter(context);
    layout.addView(sMeter);
    final Handler mHandler = new Handler();
    new Thread() {
        @Override
        public void run() {

            while (running) {
                previousTime = getCurrentTime();
                previousNum = getRawNum();
                try {
                    Thread.sleep(1500);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                currentTime=getCurrentTime();
                currentNum = getRawNum();
                mHandler.post(mUpdateResults);
            }
        }
    }.start();
}
final Runnable mUpdateResults = new Runnable() {
    public void run() {
        updateResultsInUi();
    }
};
private void updateResultsInUi() {
    if(previousNum>currentNum){
        return;//TODO
    }else{
        int temp=currentNum-previousNum;//Problem here, both num are same.
        double timeTemp=currentTime-previousTime; 
        someField=  (float)( temp/timeTemp);
    }
    sMeter.setTarget(someField);//this should be last line to be executed in this snippt
}

So the problem is previousNum and currentNum are always returning the same number. I want the thread to wait 1.5 sec to get a new (getRawNum()) from the onProgressupdate. But I don’t understand they they are returning the same number. Thanks for any advice.

  • 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-11T04:21:15+00:00Added an answer on June 11, 2026 at 4:21 am

    I don’t think this section is doing what you want:

    while (running) {
        previousTime = getCurrentTime();
        previousNum = getRawNum();
        try {
            Thread.sleep(1500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        currentTime=getCurrentTime();
        currentNum = getRawNum();
        mHandler.post(mUpdateResults);
    }
    

    For instance, let’s say this starts executing at time=0. At the end of the first iteration you will have:

    previousTime = 0
    currentTime  = 1500
    

    …however, the loop will immediately start its second iteration, and then you will have:

    previousTime = 1500
    currentTime  = 1500
    

    …then it will sleep for another 1500 milliseconds and set currentTime to 3000, and then immediately start the next iteration, setting previousTime to 3000 as well.

    So the problem is that for the vast majority of the time, your code is leaving currentTime and previousTime set to the same value. The only time they have different values is for the extremely small span of time in between when one iteration ends and the next iteration begins.

    A quick way to work around this problem is as follows:

    currentTime = getCurrentTime();
    currentNum = getRawNum();
    while (running) {
        try {
            Thread.sleep(1500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        previousTime = curentTime;
        previousNum = currentNum;
        currentTime=getCurrentTime();
        currentNum = getRawNum();
        mHandler.post(mUpdateResults);
    }
    

    A better approach might be to refactor your code to use something like a circular buffer to keep track of the last n samples and sample timestamps. Then you can simply query for a new sample at regular intervals, and when updating the UI you just compare the most recent thing in the buffer against the one that came immediately before it.

    And as for fields in your UI not updating, make sure that the code you have that tries to update them is running on the main thread.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class HttpPostTask extends AsyncTask<Void, Integer, Void> { TextView txtStatus = (TextView)findViewById(R.id.txtStatus); @Override protected
I show ProgressDialog in AsyncTask method. My code as below: @Override protected void onPreExecute()
This is my AsyncTask, called from getView of an adapter: class LoadImage extends AsyncTask<Object,Void,String>{
public class IdAsync extends AsyncTask<String, Void, Void> { AlertDialog alertDialog = new AlertDialog.Builder(MainClass.this).create(); protected
This is my AsyncTask class private class UpdatingNews extends AsyncTask<Void, Void, String> { @Override
When calling AsyncTask<Integer,Integer,Boolean> , where is the return value of: protected Boolean doInBackground(Integer... params)
I have an AsyncTask. protected class InitTask extends AsyncTask<Context, Integer, String> { View eachLayout;
I have an AsyncTask which gets called onCreate() in my Main Activity . In
Android documentation says that AsyncTask postExecute() is called on the UI thread. I was
Suppose I have a innerclass extends asynctask called A and the outerclass extends Activity

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.