logo
down
shadow

Android CountDownTimer Class Lagging Main Thread


Android CountDownTimer Class Lagging Main Thread

By : user2955982
Date : November 22 2020, 10:48 AM
Hope that helps I decided to take a different approach which has served my purposes very well. No lag or thread issues and can easily be restarted over and over. Hope this helps someone out.
code :
int startCountdown = 5;
int currentCountdown;
Handler countdownHandler = new Handler();
Timer countdownTimer = new Timer();
public void startCountdownTimer() {
    currentCountdown = startCountdown;
    for (int i = 0; i <= startCountdown; i++) {
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                countdownHandler.post(doA);
            }
        };
        countdownTimer.schedule(task, i * 1000);
    }
}
final Runnable doA = new Runnable() {
    @Override
    public void run() {
        if (currentCountdown != 0) {
            tvTextView.setText("" + currentCountdown);
        currentCountdown--;
        } else {
            currentCountdown = startCountdown;
            startCountdownTimer();
        }
    }
};


Share : facebook icon twitter icon
How to run CountDownTimer when I'm not in the main thread

How to run CountDownTimer when I'm not in the main thread


By : Hopefol
Date : March 29 2020, 07:55 AM
This might help you You can create a Timer that will start a download on your UI thread. In your UI class, use this function
code :
private StartOneHourTimer()
{
new CountDownTimer(10000 * 3600, 1000) {
     public void onTick(long millisUntilFinished) {
         mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
     }
     public void onFinish() {
         m_downloader = new Downloader(..);
         m_downloader.execute(..);
     }
  }.start();
}
protected void onPostExecute(File file)
    {
        if (!isVideoLoaded)
          startOneHourTimer();
        else
          //Call your normal UI code here
    }
If a method belongs to another class that extends Thread but is called from the main thread, will it be executed by main

If a method belongs to another class that extends Thread but is called from the main thread, will it be executed by main


By : Nucklehead
Date : March 29 2020, 07:55 AM
This might help you The toString() call is executed on the main thread. The main thread is invoking Main.main(); main() directly invokes test.toString().
Just because your output prints the string "Test" doesn't mean that's the thread that is executing it. Thread has state; setName(...) sets that state (and your class TestThread is a subclass of Test so it inherits this as well). In your toString() implementation you're just printing that state... not the actual name of the executing thread.
code :
   @Override
   public String toString() {
       return(getName() + ": x = " + x + " executed on thread " + Thread.currentThread().getName());
   }
public class Main {
       public static void main(String[] args) {
           ThreadTest test = new ThreadTest("Test", 3);
           test.start();
           System.out.println(test.toString());
       }    


}

public class ThreadTest extends Thread {
    private int x;
       public ThreadTest(String n, int x) {
           setName(n);
           this.x = x;

       }
       @Override
       public String toString() {
           return(getName() + ": x = " + x + " executed on thread " + Thread.currentThread().getName());
       }
       public void run() {
           //Nothing of any relevance to the problem occurs here
       }
}
IntentService class not running AsyncTask on main ui thread. Method execute must be called from the main thread, current

IntentService class not running AsyncTask on main ui thread. Method execute must be called from the main thread, current


By : Nicu
Date : March 29 2020, 07:55 AM
wish help you to fix your issue
Now inside the onHandleIntent(Intent intent) method i making call to an async task
Android:: Blocking main thread for worker thread to finish before executing other tasks in main thread

Android:: Blocking main thread for worker thread to finish before executing other tasks in main thread


By : Rohit Gupta
Date : March 29 2020, 07:55 AM
will be helpful for those in need You need to do things on the UI thread after worker has finished? Use runOnUiThread at the end of your worker or use AsyncTask with onPostExecute(). Blocking the UI thread while worker is running doesn't make any sense.
Using CountDownTimer from background thread in Android

Using CountDownTimer from background thread in Android


By : Tuba Ahmed
Date : March 29 2020, 07:55 AM
wish help you to fix your issue I think inorder to update a textview in activity u must use runOnUiThread eg:
Related Posts Related Posts :
  • Repeating for loop in menu
  • Can Java garbage collector randomly delete objects in the On-Heap tier?
  • Apache CXF Exception: SSL connection unexpectedly closed
  • Add properties to ArrayList<Object> in Java
  • Multiple Adapters or One Adapter for different lists and objects - Code Performance
  • Is it good practice in Java for a class's method to redundantly return a modified global field of the class?
  • How to change the color of the background in libgdx labels?
  • direct file path not working
  • ClassFileTransformer + Javassist: no such field
  • How do I change and instance variable for an object which the user selects from a combobox?
  • reading and updating a large xml file in java
  • ActionListener and Thread.sleep
  • What is the maximum of number of arguments for varargs in java?
  • When I compile I get an error as "ask" cannot be resolved or is not a field"
  • What is the best way to check if 4 integer variables are equal to 0?
  • Storing constant webdriver elements in an enum
  • Java: Why main class does not extend Thread class
  • Java - how to find out if a directory is being used by another process?
  • How can I get a method to print?
  • Java unknown exceptions
  • BufferedReader,StreamWriter crash
  • found raw type: JComboBox
  • Java Generics -> Function return type
  • how to refresh contents of jtable on event of action listener?
  • Why does List interface extend Collection interface?
  • Pros and Cons of Clojure http client libraries
  • use final inside a for each loop
  • Convert java data object to service object
  • Unsupported major.minor version 51.0 while executing JSP
  • How do I display a word diagonally in Java?
  • making sure one task completes before another starts in java
  • My program gives an index out of bounds error
  • Removing duplicate characters in a String (user inputted keyword)
  • Jersey 2.0 Content-Length not set
  • AWS.SimpleQueueService.NonExistentQueue Exception thrown when Accessing Existing SQS queue
  • Where can I find the source code for the com.sun.jdi package?
  • How can I get path of resource under WEB-INF/class folder (java ee web dynamic project)
  • convert a hexadecimal into a decimal via string convert in java
  • error populating a table using jstl
  • If Singletons are bad then how do you store global state for a framework
  • input system where the user inputs the array position of the object followed by a # to indiacte quantity but it gives me
  • Java: when would I ever want to use static methods when I can use a singleton instead?
  • Extracting RSA public key modulus and exponent stored in unsigned char arrays
  • What does "cannot be resolved to a type" mean, and how can I fix it? Java Android 4.0
  • How can I have a callback on stdout/stderr readiness instead of busy polling?
  • How to report AWT/Swing event queue length programmatically?
  • What is the difference between different for loops in Java?
  • Java arrays effective use/alternative
  • EasyMock chained method call producing null pointer exception
  • Installing Java on Windows 8
  • Java: Is it bad practice to write methods that start with "get" when they aren't getters?
  • Can Iterable.filter() skip processing with "constant" (including short circuited) Predicates?
  • How to save edited JTable data to database?
  • Java Packet Sniffer
  • I have a getText error
  • Reading bytestream and writing to file
  • dat file is not get created by buffered writer
  • Converting a pdf to word document using java
  • Measuring memory use of a piece of code at runtime in Java as an on-going functionality of a service
  • What is Matrix.frustumM(mProjMatrix, 0, -ratio, ratio, -1, 1, 3, 7); in OpenGL ES 20?
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org