0

There is a Button that start an activity every 30 second by a thread. My problem is when I start the activity I cannot cancel or pause it and it runs periodically when I go back to the previous page or even home screen and show the Intent. ans also when I come back and change some setting like loop duration and press the Button, the new thread is added to previous one and both run with their loop time.

I tried to use finish() in onPause() or interrupt but I could not solve the problem!

Thread timer = new Thread(){
public void run(){
    try {
            webWiew.loadUrl(xxxxxxx);
        sleep(30000);       
    } catch (InterruptedException e) {
        e.printStackTrace();
    }finally{
        Intent runNewIntent =new Intent("The Intent Name");
        startActivity(runNewIntent);
            }
        }
    };
    timer.start();
} 
@Override
protected void onPause() {
    super.onPause();
    finish();
}

I was wondering if you tell me how can I stop or kill the thread when I leave the activity by pressing a keyboard or return button or go out of the program.

4

1 回答 1

0

检查这个How do you kill a thread in Java?
而这个http://docs.oracle.com/javase/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html

你可以试试timer.interrupt();

于 2012-08-15T09:26:47.250 回答