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 deviceID in browser's localstorage.
Now there are couple of things that I would like to ask:
- I am showing an alert when deviceID is saved and that alert is working fine in the app but how can I see the deviceID in my phone's browser once it is saved? I am using Chrome in my phone and I tried going to Chrome > Settings > Site Settings > Storage but nothing is there.
- When user clicks on btnA, I want to run a background
serviceafter every 5 minutes which hits anapiand I need to pass the deviceID (stored in a localStorage) as aparameterfor the api. How would I get that deviceID fromlocalstoragein my android app so I can pass it as a parameter?
This is how I am loading the webview in the app:
webView = findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.setWebChromeClient(new WebChromeClient());
webView.loadUrl("file:///android_res/raw/index.html");
This is my web page:
<!DOCTYPE html>
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
</head>
<body>
<button type="submit" value="Click Me" onclick="saveToLocalStorage()">Click Me</button>
<script src="./app.js" type="text/javascript"></script>
</body>
</html>
and this is my app.js:
function saveToLocalStorage() {
var generatedString = randomString(9, '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');
if(localStorage.getItem("deviceID") == null){
localStorage.setItem("deviceID", generatedString);
alert("DeviceID saved.")
}
}
function randomString(length, chars) {
var result = '';
for (var i = length; i > 0; --i) result += chars[Math.floor(Math.random() * chars.length)];
return result;
}