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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:54:05+00:00 2026-06-01T22:54:05+00:00

I have an android app where user presses a start button and some function

  • 0

I have an android app where user presses a start button and some function of collecting data is started. I have a custom EditText that shows the time that all this process has taken and is updated every second till user presses stop. I implemented this using an AsyncTask like this below:

    protected class RecTimer extends AsyncTask<Context, Integer, String> 
    {
                @Override
                protected String doInBackground( Context... params ) 
                {
                         int sec=00;
                     int min=00;
                     int hours=00;
                     timerdata=timer.split(":");
                     hours=Integer.parseInt(timerdata[0]);
                     min=Integer.parseInt(timerdata[1]);
                     sec=Integer.parseInt(timerdata[2]);

                        while(Recording==true) 
                        {
                            try{

                                        Thread.sleep( 1000 );
                                        publishProgress( sec , min , hours );
                                        sec++;

                                        if(sec==60){

                                            min++;
                                            sec=0;

                                            if(min==60){
                                                hours++;
                                                min=0;
                                            }
                                        }
                                } catch( Exception e ){
                                        Log.i("makemachine", e.getMessage() );
                                }
                        }
                        return "COMPLETE!";
                }

                @Override
                protected void onPreExecute() 
                {
                        Log.i( "makemachine", "onPreExecute()" );
                        super.onPreExecute();                   
                }

                @Override
                protected void onProgressUpdate(Integer... values) 
                {
                        super.onProgressUpdate(values);
                        //Log.i( "makemachine", "onProgressUpdate(): " +  String.valueOf( values[0] ) );

                        String format = String.format("%%0%dd", 2);
                        String Sec = String.format(format, values[0]);
                        String Min = String.format(format, values[1]);
                        String Hours = String.format(format, values[2]);

                        ((EditText) findViewById(R.id.duration1)).setText(Hours + ":"+ Min + ":" + Sec);

                }


                @Override
                protected void onPostExecute( String result ) 
                {      
                        super.onPostExecute(result);
                        Log.i( "makemachine", "onPostExecute(): " + result );

               }          
        } 

It seemed to work fine at first, but now I see that if the app is idle and out of focus the timer may not always count well. I mean that I leave for example the device for 15 minutes and then see that in my “clock” only 5 have passed. Since the view is back on focus everything seems to work normally again.

First of all why does this happen? Shouldn’t the AsyncTask be always executed in background? Is there some other better approach I should use to implement this?

Thanks to everybody in advance!

EDIT: Thanks to TrackRuler I implemented it like this:

private Runnable Timer = new Runnable() {

  public void run() {
           long current = SystemClock.uptimeMillis();
               String format = String.format("%%0%dd", 2);
               timerdata=timer.split(":");
               sec++;

               if(sec==60){

                   min++;
                   sec=0;

                   if(min==60){
                       hours++;
                       min=0;
                   }
               }

               String Sec = String.format(format, sec);
               String Min = String.format(format, min);
               String Hours = String.format(format, hours);

               ((EditText) findViewById(R.id.duration1)).setText(Hours + ":"+ Min + ":" + Sec);


                   TimerHandler.postAtTime(this,
                          current +  1000);
                    }                       
            }; 

From my main code I call it from a handler:
TimerHandler.post(Timer);

It works by now.. I just want to know if there is a problem in updating the view inside the Runnable(?)

  • 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-01T22:54:06+00:00Added an answer on June 1, 2026 at 10:54 pm

    Basically the AsyncTask will be ended when the while completed. It wont wait for your sleep calls.

    You may have to implement the Runnable interface and do that what u needs inside the ‘Runnable’.

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

Sidebar

Related Questions

I have an Android app that allows the user to record data (such as
I have android app that talks to server and syncs some data int SQLite
I have an Android app that deals with GPS data. When a user starts
I have a dialog in an Android app that I don't want the user
Lets say you have a web app related Android app that requires the user
I need some help. In my android app I have a ViewFlipper that contains
I have a android app that is quite simple it fetches data from the
I have an Android app that looks absolutely horrible if the user sets their
Right now in my android app a user presses a button to go to
I am working on a android app and I have an EditText where user

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.