By the way, this code is located in a subclassed WebView.

On Jan 18, 9:53 am, tatebn <brandonnt...@gmail.com> wrote:
> I'm trying to evaluate javascript with a semaphore lock.  I need to
> query some web content about which context menus to show and need it
> to be synchronous.  When I try to acquire the semaphore the thread
> locks without the javascript being evaluated and my javascript
> interface method being called.
>
> Here's my code.
>
> private Semaphore returnSemaphore = new Semaphore(0);
>         private String returnValue;
>
>         public String getJSValue(String expression)
>         {
>                 String code = 
> "javascript:_JSEval.setValue((function(){try{return "
> + expression
>                         + "+\"\";}catch(js_eval_err){return '';}})());";
>                 //String code = "javascript: alert('here');";
>                 Log.d(TAG, "eval: " + code);
>
>                 this.loadUrl(code);
>
>                 try {
>                         Log.d(TAG, "eval: about to acquire semaphore, " +
> returnSemaphore.availablePermits());
>
>                         returnSemaphore.acquire();
>
>                         Log.d(TAG, "past semaphore acquire");
>
>                         return returnValue;
>                 } catch (InterruptedException e) {
>                         Log.e(TAG, "eval: Interrupted", e);
>                 }
>                 return null;
>
>         }
>
>         @SuppressWarnings("unused")
>         private class JSEval
>         {
>                 public void setValue(String value)
>                 {
>                         Log.i(TAG, "eval: in set value");
>                         //if (value != null) {
>                                 returnValue = value;
>                         //}
>                         returnSemaphore.release();
>                 }
>         }
>
> If I remove the semaphore acquire call, I get a result in setValue, so
> I know the interface is working.

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

Reply via email to