I thinks I answered my own question again.

The problem appears to be with the Android Service object.  I was
using an instance of the Service object to do my synchronization.

In normal Java, when you call wait() inside of a synchronization
block, the thread is supposed to release the monitor so that the
thread can be notified ( ... because notify() must also be called from
within a synchronization block.)

It appears the Android Service object does not behave according to
normal Java semantics i.e., it doesn't release the monitor when wait()
is called.  Therefore, notify() can't enter the monitor in order to
wake up the thread and therefore, the wait always times out.

The simple solution is to use another object as the synchronizer.  I
guess Service can be viewed as a special OS object with unique
restrictions so it's understandable.  However, this fact should
definitely be placed in the documentation - yo Google.

On Nov 17, 1:57 pm, dreamerBoy <[EMAIL PROTECTED]> wrote:
> Hi -
>
> I'm having a problem with my PhoneStateListener not being called until
> after the Phone Activity is finished.  Here is what I am doing:
>
> step 1:  Install a PhoneStateListener.  The listener is supposed to
> call notify() (synchronized on the lock object) when it detects a
> change in the phone state (specifically OFF_HOOK).
>
> step 2:  Place a call with the ACTION_CALL intent.
>
> step 3:  Call wait(timeout) (in a synchronized loop), waiting for the
> listener to wake us up.
>
> (I realize  wait/notify can be tricky but am confident the
> implementation is  correct...)
>
> Step 3 is always appearing to time out even though I am seeing the
> call go through properly on the emulator screen.  No matter what
> timeout value I use, it always times out even though
> telephonyManager.getCallState() returns the correct value (OFF_HOOK).
>
> I tried placing step 2 and step 3 in their own, separate (ordinary
> Java) threads.  Same behavior.
>
> When I tried placing step 1 in its own (ordinary Java) thread, Android
> throws:
>
> 11-17 12:12:01.451: ERROR/AndroidRuntime(2004): Uncaught handler:
> thread Thread-9 exiting due to uncaught exception
> 11-17 12:12:01.451: ERROR/AndroidRuntime(2004):
> java.lang.RuntimeException: Can't create handler inside thread that
> has not called Looper.prepare()
> 11-17 12:12:01.451: ERROR/AndroidRuntime(2004):     at
> android.os.Handler.<init>(Handler.java:111)
> 11-17 12:12:01.451: ERROR/AndroidRuntime(2004):     at
> android.telephony.PhoneStateListener$2.<init>(PhoneStateListener.java:
> 235)
> 11-17 12:12:01.451: ERROR/AndroidRuntime(2004):     at
> android.telephony.PhoneStateListener.<init>(PhoneStateListener.java:
> 234)
> 11-17 12:12:01.451: ERROR/AndroidRuntime(2004):     at
> test.phone.PhoneTestService$MyPhoneStateListener.<init>
> (PhoneTestService.java:529)
> 11-17 12:12:01.451: ERROR/AndroidRuntime(2004):     at
> test.phone.PhoneTestService$MyPhoneStateListener.<init>
> (PhoneTestService.java:529)
> 11-17 12:12:01.451: ERROR/AndroidRuntime(2004):     at
> test.phone.PhoneTestService.listenToCallState(PhoneTestService.java:
> 640)
> 11-17 12:12:01.451: ERROR/AndroidRuntime(2004):     at
> test.phone.PhoneTestService.access$2(PhoneTestService.java:618)
> 11-17 12:12:01.451: ERROR/AndroidRuntime(2004):     at
> test.phone.PhoneTestService$1.run(PhoneTestService.java:704)
> 11-17 12:12:01.451: ERROR/AndroidRuntime(2004):     at
> java.lang.Thread.run(Thread.java:935)
>
> Working on figuring out the right way to launch the listener in a
> Handler thread - however, there's nothing in the PhoneStateListener
> write-up about having to launch in a separate execution context.  This
> doesn't seem to follow Java orthodoxy either - e.g., in Swing,
> callbacks are invoked seamlessly - no separate execution context
> required.
>
> ...Unless - I've overlooked something ...
>
> Can anyone shed any light on this?
>
> Thanks much.
>
> -dreamer
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to