I have an app that displays a webview on the whole layout. Sometimes I need to call an async method, that async operation is done by a 3-party sdk, and then I wait for it for a while until I get the response to a designated listener. I have solved it with a latch - once the response is received in the listener, I countDown the latch and then the initiating method can continue with this response. Unfortunately, when I do this, the WebView is stuck. I imagined the native UI would be stuck , but I didn't expect the webview to get frozen as well. How can that be overcome?

我有一个应用程序,在整个布局上显示webview。有时我需要调用异步方法,异步操作由三方sdk完成,然后我等待一段时间,直到我得到对指定监听器的响应。我用一个锁存器解决了它 - 一旦在监听器中收到响应,我就计算掉锁存器,然后启动方法可以继续这个响应。不幸的是,当我这样做时,WebView被卡住了。我想象本机用户界面会被卡住,但我没想到webview也会被冻结。怎么能克服?

To make it clearer, here is an example. I need the ajaxFunc to wait until MyAsyncListener gets a certain response, and then return this exact response.

为了更清楚,这是一个例子。我需要ajaxFunc等待MyAsyncListener获得某个响应,然后返回这个确切的响应。

part of JS I inject to the webview :

JS注入webview的一部分:

var response = jsHandler.ajaxFunc(request.data);

I have a variable global variable called response.

我有一个名为response的变量全局变量。

public class JavaScriptInterface
{
     @JavascriptInterface
     public String ajaxFunc(String data)
     {
         return MyUtils.handleData( data );
     }
}

the handleData method :

handleData方法:

 public String handleData( String data )
 {
     SomeClass.startAsyncRequest(); // starts the async request, response is to come at the listener's callbacks .

     latch = new CountDownLatch(1);
     try {
         latch.await(30, TimeUnit.SECONDS);
     }
     catch (InterruptedException e) {
         e.printStackTrace();
     }

     return response;
   }

now, once the handleData function calls a function, that does something asynchronously, the async function then returns some answer inside some Listener :

现在,一旦handleData函数调用一个异步执行的函数,async函数就会在一些Listener中返回一些答案:

myAsyncListener = new MyAsyncListener() {
    @Override
        public Response handleEvent(CustomEvent event) {
            //Now I need to return this 'event' back to the handData/ajaxFunc function <-- 

            response = event.getName();
            latch.countDown();

        }
    });

3 个解决方案

#1


4

I need the ajaxFunc to wait until MyAsyncListener gets a certain response, and then return this exact response.

我需要ajaxFunc等待MyAsyncListener获得某个响应,然后返回这个确切的响应。

Since you are dispatching an async request, you shouldn't expect the response to arrive in the same method call you made to start the async code. If you do this, the caller thread will block until the async response arrives, that is, it won't be async at all.

由于您要分派异步请求,因此您不应期望响应会在您启动异步代码的同一方法调用中到达。如果这样做,调用程序线程将阻塞,直到异步响应到达,也就是说,它根本不会异步。

You can redesign your code to something like this:

您可以将代码重新设计为以下内容:

public class JavaScriptInterface {
    @JavascriptInterface
    public void ajaxFunc(String data) {
        // draw a waiting animation or something
        MyUtils.handleData(data);
    }
}
public void handleData(String data) {
    SomeClass.startAsyncRequest();
}
myAsyncListener = new MyAsyncListener() {
    @Override
    public void handleEvent(CustomEvent event) {
        // Do what you need to do
        // (update UI, call javascript etc.).
        //
        // Mind the thread that will execute this callback
        // to prevent multithreading issues.
    }
}

更多相关文章

  1. 四极管:I2CTools编译方法
  2. 检查ArrayList是否只包含null值的方法。
  3. 不同Android版本设备正确获取屏幕分辨率的通用方法
  4. studio更新时候,不小心点了ignore,无法更新的解决方法
  5. android之发送短信的方法研究
  6. SQLite的Android光标在方法调用时崩溃
  7. android 2D 游戏的开发的方法
  8. 回调函数在Android监听机制中的体现
  9. 在Skobbler中完成导航时,确定“街边”的最佳方法是什么?

随机推荐

  1. Android获取程序路径 (/data/data/appname
  2. Android之获取手机信息
  3. android 跳转到当前应用的详情页面
  4. Toast——多次点击只显示一次解决方案
  5. Android 获取手机信息
  6. 修改系统分辨率
  7. Android framework完整源码下载
  8. Android关闭其他程序
  9. js 与安卓或ios 的交互传参
  10. android中的两端对齐