问题描述
我公司开发的PhoneGap的应用程序为Android。该应用程序是由组成的 web应用程序的(HTML / jQuery的)和后台服务的(Java的code)这是开始的Web应用程序。
I have developed a PhoneGap application for Android.The application is composed by the webapp (HTML/jQuery) and a background service (Java code) that's started by the webapp.
该web应用写入 window.localStorage 喜欢
<script>
window.localStorage.setItem("name","MyName");
</script>
是否有可能阅读的名称那就是在localStorage的,从我的Java code?
Is it possible to read this name that is in the localStorage from my Java code?
推荐答案
这是可能的。执行JavaScript,并得到回应,你可以做如下:
This is possible. To execute JavaScript and get response you can do as follows:
定义JavaScript回调接口在code:
Define JavaScript callback interface in your code:
class MyJavaScriptInterface {
public void someCallback(String jsResult) {
// this will be passed the local storage name variable
}
}
附加此回调到您的WebView:
Attach this callback to your WebView:
MyJavaScriptInterface javaInterface = new MyJavaScriptInterface();
webView.addJavascriptInterface(javaInterface, "HTMLOUT");
运行你的JavaScript调用window.HTMLOUT.someCallback从脚本:
Run your JavaScript calling window.HTMLOUT.someCallback from the script:
webView.loadUrl("javascript:( function () { var name = window.localStorage['name']; window.HTMLOUT.someCallback(name); } ) ()");
请注意 - window.localStorage ['名称']
是一样的 window.localStorage.getItem('名称')
Note - window.localStorage['name']
is the same as window.localStorage.getItem('name')
This张贴在这里的计算器帮我
您可能需要使用 super.webView.addJavascriptInterface
或 super.addJavascriptInterface
添加接口。您可能需要使用 super.webView.loadUrl
或 super.loadUrl
调用此方法。这一切都取决于你要去哪里从调用这些。
You might need to use super.webView.addJavascriptInterface
or super.addJavascriptInterface
to add the interface. You might need to use super.webView.loadUrl
or super.loadUrl
to invoke this. It all depends on where you are going to be calling these from.
这篇关于Android的服务读取localStorage的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!