WebView doesn't evaluate the JavaScript synchronously.  I need an
immediate return not a call to a separate interface method.  The only
method I've found of anyone evaluating javascript synchronously is
using this semaphore method, but I can't get the javascript to
continue evaluating when the semaphore is locked.  I've tried running
the javascript evaluation code in an async task as well, still no
lock.

On Jan 18, 10:32 am, Daniel Drozdzewski <daniel.drozdzew...@gmail.com>
wrote:
> Why can't you just let webview evaluate the JavaScript?
>
> On 18 January 2012 15:18, tatebn <brandonnt...@gmail.com> wrote:
>
>
>
>
>
>
>
>
>
> > I need the semaphore to hold there so that the return doesn't happen
> > until the value is sent back from javascript.  How do I get the web
> > view to continue evaluating the javascript in the background?
>
> > On Jan 18, 10:05 am, Daniel Drozdzewski <daniel.drozdzew...@gmail.com>
> > wrote:
> >> Your semaphore allows ZERO passes through, in which case you have to
> >> release first.
>
> >> Either change the parameter to Semaphore constructor or release before
> >> calling Semaphore.acquire()
>
> >> Daniel
>
> >> On 18 January 2012 15:00, tatebn <brandonnt...@gmail.com> wrote:
>
> >> > 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
>
> >> --
> >> Daniel Drozdzewski
>
> > --
> > 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
>
> --
> Daniel Drozdzewski

-- 
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