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.

Ramtin
  • 1

1 Answers1

0

Check this How do you kill a thread in Java?
And this http://docs.oracle.com/javase/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html

You could try timer.interrupt();

Community
  • 1
  • 1
Halle
  • 11
  • 2
  • I did timer.interrupt(); and Thread.currentThread().interrupt(); in onPause() also but unfortunately they do not work! – Ramtin Aug 22 '12 at 13:58
  • timer.interrupt(); works in onPause() when create and start the Activity in the try instead of finally. Thanks. – Ramtin Aug 31 '12 at 13:45