Skip to content Skip to sidebar Skip to footer

Android/java - Pause Thread

I'm new to this, so maybe it's trivial to everybody, but I just can't figure out, why this isn't working. I've read about it, tried many way, and still not working. So I want to pa

Solution 1:

You should change the order of the methods call, you coded:

    runner.join();
    runner.start();

Change to:

runner.start();
runner.join();

And it should work.

Solution 2:

Call Thread.sleep in the onCreate method. Throw out all the thread stuff. It is just wrong.

Why at all you want to freeze the screen? This is a very bad approach. Use Dialog instead.

Solution 3:

if you need freeze screen, you only need use SystemClock.sleep(millis).//1000 = 1 second Android use a main thread for your interface very different . Your code try stop it like a java program.

Example

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    Toolbartoolbar= (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    SystemClock.sleep(1000);

...

or search info like this:

//this is similar to your threadnewThread(newRunnable() {
       @Overridepublicvoidrun() {
           //some code
           int val = 1+ 1; //code here dont interrupt main thread//this code run on main thread
           mActivity.runOnUiThread(newRunnable() {
               @Overridepublicvoidrun() {
                   //freeze ViewsSystemClock.sleep(1000);// 1 second
               }
           });

       }
   });

Solution 4:

use this

delayprg(3000);//3seg

privatevoiddelayprg(int delayc) {
        try {
            Thread.sleep(delayc);
        } catch (InterruptedException e) {
        }
    }

Solution 5:

You must avoid spinning try this:

privateboolean isPaused = false;


public synchronized voidpause(){
    isPaused = true;
}

public synchronized voidplay(){
   isPaused = false;
   notifyAll();
}

public synchronized voidlook(){
   while(isPaused)
      wait();
}

 publicvoidrun(){
     while(true){
        look();
        //your code
 }

Post a Comment for "Android/java - Pause Thread"