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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:28:18+00:00 2026-05-25T22:28:18+00:00

After many trials of solutions (including posting questions on SO) I thouht will work

  • 0

After many trials of solutions (including posting questions on SO) I thouht will work fine. But no improvement. Coming to my problem, I am implementing an App that have a countdown timer. I am showing this on a button (Just using like a canvas by disabling the click event).
I start the timer when user clicks a button (which is a separate button). Below is the countdown timer code,

public class DigitalTimer extends Button{

--------------------------------    
--------------------------------    
--------------------------------    
--------------------------------    
--------------------------------    
--------------------------------    
private String timerText;
public DigitalTimer (Context context,int hourstime,int mintime,int sectime){
    super(context);
    Log.d(TAG,"DigiTimer constructor");     
    this.context = context;     
    initialize(hourstime,mintime,sectime,timerType);
    setClickable(false);
}

public void initialize(int hourstime,int mintime,int sectime,int timerType){
    Log.d(TAG,"DigiTimer initialize");      
    this.hourstime = hourstime;
    this.mintime = mintime;     
    hour = hourstime;
    min = mintime;
    sec = sectime;

    //Just Thread version               
    **digiThread = new Thread(){
        @Override
        public void run(){
            while(!isPauseTimer()){
                updateTimes();
                SystemClock.sleep(UPDATEDELAY);
            }

        }           
    };**


    //Handler version       

    /*
    handleRunnable = new Runnable(){
        public void run(){
            updateTimes();
        }
    };
    */

}

private void updateTimes(){

    timerText = String.format(timerFormat,hour,min,sec );       
    postInvalidate(); 
    sec--;
    if(sec < 0){
        sec = 59;
        min--;
    }
    if(min < 0){
        min = 59;
        hour--;
    }   

    if(hour < 0){  //when hour is negative it means the given time completed so we stop the timer & alarm permanantely 
     hour = 0;
     min = 0;
     sec = 0;
          }

}
@Override
protected void onDraw(Canvas canvas){
    super.onDraw(canvas);
    Log.d(TAG,"DigiTimer onDraw");      
    setBackgroundResource(R.drawable.button9patch);     
    setText(timerText);     
}
public void startUpdateTheread(){
    //digiHandler.postDelayed(handleRunnable, UPDATEDELAY);
    **digiThread.start();**
}


private void startTimersAndAlarms(){
 -----------------------
 -----------------------
 -----------------------
 -----------------------
 **startUpdateTheread();**
 ---------------------
 ---------------------

}

}

Initially the timer is woking fine . But if no.of hours for the countdown is higher (say 5:00:00) then its running fine until sometime (say 4:00:00) from then it is delaying the timer update. (just to countdown a minute it is taking more time ..particularly when the user is out of the App)
First I tried with the Handler. I had the problem. I thought the delay is because of keeping the UI thread busy. So I developed a separate thread. But still the problem persist.

Sorry for the long post. Please someone point what’s happening. Is that something I am missing or putting the code in wrong place?

Thanks

EDIT: I read SystemClock.sleep documentation. It says “{This clock stops when the system enters deep sleep (CPU off, display dark, device waiting for external input) }”. I understand that I should keep CPU on while I run this thread. So according to the answer by @Brandon I should implement partial POWERLOCK to keep the CPU on. Is my understanding correct?

  • 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-05-25T22:28:18+00:00Added an answer on May 25, 2026 at 10:28 pm

    If you want to have an accurate timer, I think you’ll need to get a wake lock.

    Check out the documentation of PowerManager

    You can either get a FULL_WAKE_LOCK to keep the screen on, or a PARTIAL_WAKE_LOCK to keep the CPU running. Don’t forget to add the permission to your AndroidManifest file too.

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

Sidebar

Related Questions

I saw many questions about this, and tried to solve the problem, but after
im trying to customize this page indicator with half succes, but after many hours
I have what I know is a simple question, but after many searches in
I have refereed old questions and found that people face many issues after installing
I'm trying to use GitHub for the first time, but after many issues trying
I have a problem with my code (obviously) and after many searches on Internet,
After many search on the subject I finally think the solution of my problem
I am trying to install Boost 1.47.0 using cmake, after many failed trials with
After many sites searched and googling, I can't find anything for my problem. I
After many hours wasted, I officially turn to the experts for help! My problem

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.