These approaches are not working for me.  I'm not sure why, and I've
reviewed a number of implementations.  But when running this pattern
on either the emulator or my device (through the debugger), I see that
the static getLock(...) call is made from different DalikVM PIDs.

My LogCat output shows that these call, on separate PIDs, apparently
do not share the same static variables. For instance, if you look at
your code:


On Dec 11, 6:06 am, MrChaz <[email protected]> wrote:
>         synchronized public static PowerManager.WakeLockgetLock(Context
> context) {
>                 if (_wakeLock == null) {
>                         //Log.i("DownloadService", "CreatingwakeLock");
>                         PowerManager 
> mgr=(PowerManager)context.getSystemService
> (Context.POWER_SERVICE);
>
>                         
> _wakeLock=mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
>                                                                               
>                                   "my worker wake lock");
>                         _wakeLock.setReferenceCounted(false);
>                 }
>
>                 return(_wakeLock);
>         }

If you comment in that Log.i(...), I think you will find that the log
entries are from different PIDs.  Theoretically, when this occurs, the
_wakeLock static variable will be completely separate.  In all of my
examples, the I end up getting an "under-locked" exception when
releasing the WakeLock as two separate locks are created.

Here's my LogCat output.

12-11 10:50:09.948: INFO/apitest(238): Received intent broadcast
12-11 10:50:09.969: INFO/apitest(238): getLock intent called
12-11 10:50:09.969: INFO/apitest(238): creating intent lock
12-11 10:50:10.008: INFO/apitest(238): Aquiring intent lock WakeLock
{43cfd1a8 held=false, refCount=0}
12-11 10:50:10.218: INFO/apitest(213): onHandlerIntent
12-11 10:50:10.248: INFO/apitest(213): getLock intent called
12-11 10:50:10.258: INFO/apitest(213): creating intent lock
12-11 10:50:10.320: INFO/apitest(213): Releasing intent lock WakeLock
{43ba5aa0 held=false, refCount=0}
12-11 10:50:16.578: DEBUG/AndroidRuntime(213): Shutting down VM
12-11 10:50:16.578: WARN/dalvikvm(213): threadid=3: thread exiting
with uncaught exception (group=0x4001b188)
12-11 10:50:16.578: ERROR/AndroidRuntime(213): Uncaught handler:
thread main exiting due to uncaught exception
12-11 10:50:16.789: ERROR/AndroidRuntime(213):
java.lang.RuntimeException: Unable to start service
org.blandsite.apitest.apitestintentserv...@43ba4e80 with Intent
{ cmp=org.blandsite.apitest/.APITestIntentService }:
java.lang.RuntimeException: WakeLock under-locked apitest.Static
12-11 10:50:16.789: ERROR/AndroidRuntime(213):     at
android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2867)
12-11 10:50:16.789: ERROR/AndroidRuntime(213):     at
android.app.ActivityThread.access$3500(ActivityThread.java:119)
12-11 10:50:16.789: ERROR/AndroidRuntime(213):     at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1911)
12-11 10:50:16.789: ERROR/AndroidRuntime(213):     at
android.os.Handler.dispatchMessage(Handler.java:99)
12-11 10:50:16.789: ERROR/AndroidRuntime(213):     at
android.os.Looper.loop(Looper.java:123)
12-11 10:50:16.789: ERROR/AndroidRuntime(213):     at
android.app.ActivityThread.main(ActivityThread.java:4338)
12-11 10:50:16.789: ERROR/AndroidRuntime(213):     at
java.lang.reflect.Method.invokeNative(Native Method)
12-11 10:50:16.789: ERROR/AndroidRuntime(213):     at
java.lang.reflect.Method.invoke(Method.java:521)
12-11 10:50:16.789: ERROR/AndroidRuntime(213):     at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
(ZygoteInit.java:860)
12-11 10:50:16.789: ERROR/AndroidRuntime(213):     at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
12-11 10:50:16.789: ERROR/AndroidRuntime(213):     at
dalvik.system.NativeStart.main(Native Method)
12-11 10:50:16.789: ERROR/AndroidRuntime(213): Caused by:
java.lang.RuntimeException: WakeLock under-locked apitest.Static
12-11 10:50:16.789: ERROR/AndroidRuntime(213):     at
android.os.PowerManager$WakeLock.release(PowerManager.java:304)
12-11 10:50:16.789: ERROR/AndroidRuntime(213):     at
android.os.PowerManager$WakeLock.release(PowerManager.java:279)
12-11 10:50:16.789: ERROR/AndroidRuntime(213):     at
org.blandsite.apitest.WakefulIntentService.onStart
(WakefulIntentService.java:54)
12-11 10:50:16.789: ERROR/AndroidRuntime(213):     at
org.blandsite.apitest.APITestIntentService.onStart
(APITestIntentService.java:19)
12-11 10:50:16.789: ERROR/AndroidRuntime(213):     at
android.app.IntentService.onStartCommand(IntentService.java:73)
12-11 10:50:16.789: ERROR/AndroidRuntime(213):     at
android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2858)
12-11 10:50:16.789: ERROR/AndroidRuntime(213):     ... 10 more
12-11 10:50:16.969: INFO/Process(62): Sending signal. PID: 213 SIG: 3

* Note that the Receiver is running on PID 238, but the Service is
running on PID 213.

Order of events as far as I can tell:

1. Receiver creates lock through static call, which calls getLock
(...).  Static calls aquires lock
2. Receiver starts Service
3. Service calls getLock(...), which sees the _wakeLock variable as
null, creates a second WakeLock (this step does NOT aquire the lock as
it assumes the lock is already aquired in step 1)
4. Service performs it's work and calls release() <-- Exception
because WakeLock in step 3 wasn't aquired (refCount=0)

This is where things get really confusing for me.  If this pattern
indeed doesn't work, then why doesn't EVERYONE get the under-locked
exception.  I /must/ be doing something wrong.  Let me get my code up
to github and let you have a look yourself.  Running the small
application should show you the issue as I am reporting almost
immediately.

-- 
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]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to