*So I have a Thread that holds a sleep method and I think that is the problem because my program crashes when I try to stop it using the stop() method. I’m trying to stop the Thread entirely when the user hits the back button (the hardware back button). How should I go about this? Should I use something other than the sleep method? *
Thread timer2 = new Thread() { // Threads - do multiple things
public void run() {
try {
sleep(5000); // 5000 mil secs = 5 secs . sleeps thread
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
cdt2.start();
}
}
};
timer2.start(); // starts thread
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
timer1.stop(); //error
timer2.stop(); //error
cdt.cancel();
cdt2.cancel();
Intent menu = new Intent("android.intent.action.MENU");
startActivity(menu);
return true;
}
return false;
}
LOG CAT:
06-20 13:08:58.006: D/dalvikvm(22050): GC_CONCURRENT freed 951K, 7% free 15393K/16455K, paused 2ms+3ms
06-20 13:08:58.944: D/dalvikvm(22050): GC_CONCURRENT freed 700K, 6% free 15533K/16455K, paused 2ms+1ms
06-20 13:09:01.733: D/dalvikvm(22050): GC_CONCURRENT freed 955K, 7% free 15630K/16647K, paused 2ms+2ms
06-20 13:09:02.069: D/dalvikvm(22050): GC_CONCURRENT freed 880K, 7% free 15707K/16711K, paused 2ms+2ms
06-20 13:09:02.678: D/dalvikvm(22050): GC_CONCURRENT freed 762K, 5% free 15940K/16775K, paused 2ms+2ms
06-20 13:09:03.303: D/AccountTypeManager(21214): Registering external account type=ro.weednet.contactssync, packageName=ro.weednet.contactssync
06-20 13:09:03.311: D/AccountTypeManager(21214): Registering external account type=com.skype.contacts.sync, packageName=com.skype.raider
06-20 13:09:03.311: W/ResourceType(21214): getEntry failing because entryIndex 1377 is beyond type entryCount 189
06-20 13:09:03.311: W/ResourceType(21214): Failure getting entry for 0x7f020561 (t=1 e=1377) in package 0 (error -2147483647)
06-20 13:09:03.311: W/ResourceType(21214): getEntry failing because entryIndex 1378 is beyond type entryCount 189
06-20 13:09:03.311: W/ResourceType(21214): Failure getting entry for 0x7f020562 (t=1 e=1378) in package 0 (error -2147483647)
06-20 13:09:03.311: W/ResourceType(21214): getEntry failing because entryIndex 1379 is beyond type entryCount 189
06-20 13:09:03.311: W/ResourceType(21214): Failure getting entry for 0x7f020563 (t=1 e=1379) in package 0 (error -2147483647)
06-20 13:09:03.311: W/ResourceType(21214): getEntry failing because entryIndex 1778 is beyond type entryCount 189
06-20 13:09:03.311: W/ResourceType(21214): Failure getting entry for 0x7f0206f2 (t=1 e=1778) in package 0 (error -2147483647)
06-20 13:09:03.311: D/AccountTypeManager(21214): Registering external account type=com.yahoo.mobile.client.share.sync, packageName=com.yahoo.mobile.client.android.mail
06-20 13:09:03.319: D/AccountTypeManager(21214): Registering external account type=com.facebook.auth.login, packageName=com.facebook.katana
06-20 13:09:03.319: W/ResourceType(21214): getEntry failing because entryIndex 344 is beyond type entryCount 189
Your not suppose to call stop() on a runnable. Instead you need to have the Thread return.
To do this all you need is a flag that you can set to true. In this case you have mStopThread set to false and when you can to kill your thread just set it to true and it’ll return.
However, I usually see this in some sort of loop with a smaller sleep time.
If you don’t chose to modify the code example to make a tighter loop than you can always wake up your thread by calling timer2.interrupt() and then getting it to return.