Save Data To Localstorage And Then Retrieve It Using Android Java
I am loading a local web page in my android app using WebView and my web page has one button (lets say 'btnA'). When user clicks btnA, javascript function is called which saves dev
Solution 1:
You could create a Javascriptinterface:
publicclassJavaScriptInterface {
Context mContext;
/** Instantiate the interface and set the context */JavaScriptInterface(Context c) {
mContext = c;
}
@JavascriptInterfacepublicvoiddeviceId(String deviceId) {
// Do whatever you want with the deviceId
}
}
And pass it to the webview:
webView.addJavascriptInterface(newJavaScriptInterface(this), "injectedObject");
Then you can send that id to the native app:
<scripttype="text/javascript">functionsendDeviceId() {
injectedObject.deviceId(localStorage.getItem("deviceID"));
}
</script>
By calling sendDeviceId()
from inside the website the native app should receive the stored deviceId. This could be done at the end of saveToLocalStorage()
method.
To check the localstorage you can open the website on your smartphone in chrome browser and then connect it to a computer and visit chrome://inspect/#devices
from the computer. This should show you opened tabs and there is a link to inspect in there and this will open the website inspector where you can check the local storage with that website in the Application tab.
Post a Comment for "Save Data To Localstorage And Then Retrieve It Using Android Java"