localStorage的HTML5功能未三星的Andr​​oid设备上工作的WebView功能、设备、工作、localStorage

2023-09-05 08:38:40 作者:长安下雪了吗

我有我包装了的WebView一个HTML5应用程序。要在页面之间存储和检索用户输入的值,我使用的localStorage HTML5功能。

它工作正常,在我的Nexus 4(安卓4.4.4),但它不会对三星Galaxy Tab 2(安卓版本4.3.x)工作(=没有任何反应,而且在logcat中没有错误)。 或者,更清楚:三星如果HTML页面从应用程序的资源文件夹中加载,这是行不通的。它的工作,虽然如果我在outcommented线把服务器上的页面,如下图所示。

然而,在Nexus 4,从文件中加载:/// android_asset /并且如果我打开一个桌面浏览器(Chrome浏览器,火狐)的页面从文件://路径,它也正在

更新1 :我只是谁与LG电子装置报告的问题其他用户,因此它似乎并没有成为三星的具体

更新2 :存储和装载值从localStorage的正常工作,在同一页上的所有设备,但没有什么不同页面之间。在我的例子,我可以存储和检索01_home.html的价值,但是当我去的android_asset文件夹中的另一页,我不能读它了(对LG,三星的设备)。工作正常的Nexus 4,但。

下面是Web视图的设置。

  web视图=(web视图)this.findViewById(R.id.webView);
    webViewClient =新MyWebViewClient(本);
    webViewClient.setSm(SM);
    webView.setWebViewClient(webViewClient);
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(真正的);
    webSettings.setDomStorageEnabled(真正的);
    webView.getSettings()setJavaScriptEnabled(真)。
    webView.getSettings()setDomStorageEnabled(真)。
    。webView.getSettings()setPluginState(WebSettings.PluginState.ON);
    webView.getSettings()setAppCacheEnabled(假)。
    。webView.getSettings()setCacheMode(WebSettings.LOAD_NO_CACHE);
    。webView.getSettings()setUseWideViewPort(真正的);
    。webView.getSettings()setLoadWithOverviewMode(真正的);
    。webView.getSettings()setBuiltInZoomControls(假);
    。webView.getSettings()setSupportZoom(假);
    webView.getSettings()setDefaultZoom(WebSettings.ZoomDensity.FAR)。

    webView.loadUrl(文件:///android_asset/01_home.html); //不行!
    // webView.loadUrl(http://192.168.178.33/01_home.html); //没有工作!
 

本地存储code。在网页:

  //存储
。VAR数据=的document.getElementById(数据)的价值;
window.localStorage.setItem((1),数据);

// 阅读
。的document.getElementById(数据)值= window.localStorage.getItem(1);
 

解决方案

我发现了同样的错误。曾在所有的Nexus设备,但所有三星设备丢失了我的用户更改。我知道下面的方法是德precated ,但它解决了我的问题。

  webview.getSettings()setDatabasePath(/数据/数据​​/+ webview.getContext()getPackageName()+/数据库/)。
 

I have a html5 application that I wrap with a WebView. To store and retrieve user input values between pages, I use the localStorage html5 feature.

It works fine on my Nexus 4 (Android 4.4.4), but it does not work on Samsung Galaxy Tab 2 (Android 4.3.x) (= nothing happens, but also no error in logcat). Or, to be more clear: on Samsung, it does not work if the html pages are loaded from within the app's asset folder. It does work though if I put the pages on a server, as below in the outcommented line.

However, on Nexus 4, loading from file:///android_asset/ and also if I load the pages on a desktop browser (Chrome, Firefox) from file:// path, it is also working.

Update 1: I just had another user who reported the issue with a LG device, so it does not seem to be Samsung specific.

Update 2: Storing and loading the value from localStorage works fine on all devices on the same page, however, not between different pages. In my example, I can store and retrieve the value on 01_home.html, but when I go to another page in the android_asset folder, I cannot read it anymore (on LG, Samsung devices). Works fine on Nexus 4 though.

Below are the settings of the web view.

    webView = (WebView)this.findViewById(R.id.webView);
    webViewClient = new MyWebViewClient(this);
    webViewClient.setSm(sm);
    webView.setWebViewClient(webViewClient);
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setDomStorageEnabled(true);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setDomStorageEnabled(true);
    webView.getSettings().setPluginState(WebSettings.PluginState.ON);
    webView.getSettings().setAppCacheEnabled(false);
    webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
    webView.getSettings().setUseWideViewPort(true);
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.getSettings().setBuiltInZoomControls(false);
    webView.getSettings().setSupportZoom(false);
    webView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);

    webView.loadUrl("file:///android_asset/01_home.html"); // does NOT work!
    // webView.loadUrl("http://192.168.178.33/01_home.html"); // does work!

Local storage code in the pages:

// storing
var data = document.getElementById('data').value;
window.localStorage.setItem((1), data);

// reading
document.getElementById('data').value = window.localStorage.getItem(1);

解决方案

I found this same bug. Worked on all Nexus devices but all Samsung devices lost my user changes. I know the below method is deprecated but it solved my issue.

webview.getSettings().setDatabasePath("/data/data/" + webview.getContext().getPackageName() + "/databases/");

 
精彩推荐