Android中断睡眠线程

时间:2014-01-23 18:03:24

标签: android multithreading sleep interrupt

感谢您帮助我打断Android / Java睡眠。我的布局中有一个按钮,如果单击该按钮,则调用Skip方法并启动一个新活动。 FYI当Sleep方法终止时,无论如何都会调用相同的活动。

这是我的失败的代码:

public class Splash extends Activity {
private Thread timer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash_screen);
    timer = new Thread() {
        public void run() {
            try {
                sleep(5000);
            } catch (Exception e) {
                e.printStackTrace();
                onPause();
                return;

            } finally {
                onPause();
                startActivity(new Intent("net.example.splashscreenexample.MainActivity"));
            }
        }
    };
    timer.start();
}

@Override
public void onPause() {
    timer.interrupt();
    super.onPause();
    finish();
}

public void Skip() {
    timer.interrupt();
    startActivity(new Intent("net.example.splashscreenexample.MainActivity"));
}

现已解决!

我现在已经完成了所有工作。除了@ RocketSpock的建议之外,我的代码中还有一个愚蠢的错误,因为我没有将View视图参数包含到我的Skip方法调用中。所以完全可用的代码现在看起来像这样:

public class Splash extends Activity {
    private Thread timer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_screen);
        timer = new Thread() {
            public void run() {
                synchronized (this) {
                    try {
                        sleep(5000);
                    } catch (Exception e) {
                        e.printStackTrace();
                        onPause();
                        return;

                    } finally {
                        onPause();
                        startActivity(new Intent(
                                "net.rogw.splashscreenexample.MainActivity"));
                    }
                }

            }
        };
        timer.start();
    }

    @Override
    public void onPause() {
        timer.interrupt();
        super.onPause();
        finish();
    }

    public void Skip(View view) {
        synchronized (this) {
            this.notify();
        }
        startActivity(new Intent("net.rogw.splashscreenexample.MainActivity"));
    }
}

1 个答案:

答案 0 :(得分:1)

如果你想要打断它,你应该等待。

public class Splash extends Activity {
private Thread timer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash_screen);
    timer = new Thread() {
        public void run() {
            synchronized(this) {
                try {
                    wait(5000);
                } catch (Exception e) {
                    e.printStackTrace();
                    onPause();
                    return;
                } finally {
                    onPause();
                    startActivity(new Intent("net.example.splashscreenexample.MainActivity"));
                }
            }
        }
    };
    timer.start();
}

@Override
public void onPause() {
    timer.interrupt();
    super.onPause();
    finish();
}

public void Skip() {
    //You may need to replace this with the timer object
    synchronized (this) {
        //Informs the wait to interrupt.
        this.notify();
    }
    startActivity(new Intent("net.example.splashscreenexample.MainActivity"));
}