Understanding Android's webview addjavascriptinterface

后端 未结 3 1294
感动是毒
感动是毒 2020-12-08 15:38

I know that to interact from Javascript to Java you have to inject a Java object using the addjavascriptInterface method in webview.

Here is the problem I am facing.

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-08 16:00

    Ensure your Javascript objects declared in your HTML / Javascript that you need to access from Java are declared global otherwise they will most likely be collected. I have code that does this (where Android is my interface added with addJavascriptInterface):

    
    

    The getLocation method invokes Android's LocationManager.requestSingleUpdate which then invokes the callback when the LocationListener fires.

    Without the "var" I find that by the time the location lookup invokes the callback the callback function has been garbage collected.

提交回复
热议问题