将数据保存到 LocalStorage,然后使用 android Java 检索它 [英] Save data to LocalStorage and then retrieve it using android Java

查看:30
本文介绍了将数据保存到 LocalStorage,然后使用 android Java 检索它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 WebView 在我的 android 应用程序中加载本地网页,并且我的 网页 有一个按钮(可以说btnA").当用户点击 btnA 时,会调用 javascript 函数,将 deviceID 保存在浏览器的 localstorage 中.

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.

现在我想问几件事:

  1. 我在保存 deviceID 时显示警报,并且该警报在应用程序中工作正常,但是一旦保存 deviceID,我如何在手机浏览器中看到它?我在手机中使用 Chrome,我尝试转到 Chrome > 设置 > 站点设置 > 存储,但什么也没有.
  2. 当用户点击 btnA 时,我想在每 5 分钟命中一个 api 后运行一个后台 service 并且我需要传递 deviceID(存储在 localStorage 中)) 作为 api 的 parameter.我如何从我的 android 应用程序中的 localstorage 获取该 deviceID,以便我可以将其作为参数传递?
  1. 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.
  2. When user clicks on btnA, I want to run a background service after every 5 minutes which hits an api and I need to pass the deviceID (stored in a localStorage) as a parameter for the api. How would I get that deviceID from localstorage in my android app so I can pass it as a parameter?

这就是我在应用程序中加载 webview 的方式:

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");

这是我的网页:

<!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>

这是我的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;
}

推荐答案

您可以创建一个 Javascript 接口:

You could create a Javascriptinterface:

public class JavaScriptInterface {
    Context mContext;

    /** Instantiate the interface and set the context */
    JavaScriptInterface(Context c) {
        mContext = c;
    }

    @JavascriptInterface
    public void deviceId(String deviceId) {
        // Do whatever you want with the deviceId
    }
}

并将其传递给 webview:

And pass it to the webview:

webView.addJavascriptInterface(new JavaScriptInterface(this), "injectedObject");

然后您可以将该 ID 发送到本机应用程序:

Then you can send that id to the native app:

<script type="text/javascript">
    function sendDeviceId() {
        injectedObject.deviceId(localStorage.getItem("deviceID"));
    }
</script>

通过从网站内部调用 sendDeviceId(),本机应用程序应接收存储的 deviceId.这可以在 saveToLocalStorage() 方法的末尾完成.

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.

要检查本地存储,您可以在智能手机上的 chrome 浏览器中打开网站,然后将其连接到计算机并从计算机访问 chrome://inspect/#devices.这应该会显示您打开的选项卡,并且在那里有一个检查链接,这将打开网站检查器,您可以在其中检查应用程序选项卡中该网站的本地存储.

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.

这篇关于将数据保存到 LocalStorage,然后使用 android Java 检索它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆