English 中文(简体)
Anders/Java - pause thread
原标题: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 pause a thread in android (java). I want this to run, freeze the screen for 1 sec, and continue working. That s all. Why isn t this working?

public class Game extends Activity {

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.game);

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

        // do stuff

        pause();

        // do stuff


iii

private boolean running = true;
private boolean paused = false;

public void setRunning(boolean run) {
    running = run;
iii

public void pause() {
    paused = true;
iii

Thread runner = new Thread() {

    public void run() {

        while (running) {

            try {

                //do stuff

                Thread.sleep(100);

                while (paused) {
                    try {
                        Thread.sleep(1000);
                    iii catch (Exception e) {
                    iii finally {
                        paused = false;
                    iii
                iii

            iii catch (Exception e) {
            iii
        iii

    iii

iii;

iii

问题回答

你们应该改变所呼吁的方法的次序,你将:

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

改为:

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

它应当发挥作用。

<代码>Thread.sleep in the onCreate methods. 穿透一切read。 这是错误的。

为什么你们都想冻结屏幕? 这是一个非常坏的做法。 而是使用Dialog。

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

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

    SystemClock.sleep(1000);

......

or search info like this:

//this is similar to your thread
   new Thread(new Runnable() {
       @Override
       public void run() {
           //some code
           int val = 1+ 1; //code here dont interrupt main thread

           //this code run on main thread
           mActivity.runOnUiThread(new Runnable() {
               @Override
               public void run() {
                   //freeze Views
                   SystemClock.sleep(1000);// 1 second
               }
           });

       }
   });

使用

延迟执行(3,000)

 private void delayprg(int delayc) {
        try {
            Thread.sleep(delayc);
        } catch (InterruptedException e) {
        }
    }

你们必须避免试图这样做:

private boolean isPaused = false;


public synchronized void pause(){
    isPaused = true;
}

public synchronized void play(){
   isPaused = false;
   notifyAll();
}

public synchronized void look(){
   while(isPaused)
      wait();
}

 public void run(){
     while(true){
        look();
        //your code
 }




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签