> I'm having the following problem. I'm having an activity that starts a
> background thread in order to bind to a service which does some work.
> Now I've got the problem that when pressing the back button I get the
> following exception:
>
> 08-22 17:09:46.460: ERROR/AndroidRuntime(26218): Uncaught handler:
> thread Thread-8 exiting due to uncaught exception
> 08-22 17:09:46.460: ERROR/AndroidRuntime(26218):
> java.lang.IllegalArgumentException: Service not
> registered: [EMAIL PROTECTED]
>
> I've done the following in the onPause() method:
>
> this.unbindService(svcConn);
> background.interrupt();
> background = null;

"...the method Thread.interrupt() does not interrupt a running thread.
What the method actually does is to throw an interrupt if the thread is
blocked, so that it exits the blocked state. More precisely, if the thread
is blocked at one of the methods Object.wait, Thread.join, or
Thread.sleep, it receives an InterruptedException, thus terminating the
blocking method prematurely."

(from http://articles.techrepublic.com.com/5100-10878_11-5144546.html)

Lacking most of your source, it's hard to say specifically how to handle
things given your current setup. I prefer to use something out of
java.util.concurrent as a means of indicating to a thread that it should
shut down, though you may be able to get by with just a flag and
Thread#interrupt().

For example, in (*cough*) my book, I have an example service, with
associated activity client, for polling POP3 and IMAP mailboxes for new
messages and displaying a Notification when messages arrive. In that case,
the background thread is in the service, polling the inboxes at a
specified frequency. Of course, that thread needs to end when needed.

The way I set that up was to use a
java.util.concurrent.LinkedBlockingQueue, which the thread monitors for
work. Objects are popped on that queue based on a timer (meaning to go
poll the inboxes) and on shutdown. When a shutdown message arrives, the
service's background thread just falls out the bottom of the
watch-the-queue loop, causing the Thread to terminate on its own.

Even this may not be the best hunk of code -- it's a book sample, not a
heart monitor or nuclear power plant control system -- but I think it's in
the ballpark.

Java Concurrency in Practice (ISBN-10: 0321349601) is well-regarded for
multi-threaded programming and covers the classes in the
java.util.concurrent package.

--
Mark Murphy (a Commons Guy)
http://commonsware.com
_The Busy Coder's Guide to Android Development_ Version 1.1 Published!



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
Announcing the new Android 0.9 SDK beta!
http://android-developers.blogspot.com/2008/08/announcing-beta-release-of-android-sdk.html
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to