javascript 使用 webview 时,Android 上的 HTML localStorage 为 null
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8701015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HTML localStorage is null on Android when using webview
提问by Ali
I am trying to use localstorage from my local web APP, here is the WebView initialization part:
我正在尝试从本地 Web APP 使用 localstorage,这是 WebView 初始化部分:
static class MyWebView extends WebView
{
public MyWebView(Context context)
{
super(context);
this.getSettings().setJavaScriptEnabled(true);
//enable support for DOM Storage and Database
this.getSettings().setDatabaseEnabled(true);
this.getSettings().setDomStorageEnabled(true);
String databasePath = context.getDir("database", Context.MODE_PRIVATE).getPath();
this.getSettings().setDatabasePath(databasePath);
this.setVerticalScrollbarOverlay(true);
} }
and Here is the test app on JavaScript:
这是 JavaScript 上的测试应用程序:
function testStorage()
{
var storage = window.localStorage;
if (storage) {
try {
storage.setItem("name", "Hello World!"); //saves to the database, “key”, “value”
} catch (e) {
alert(e.message); //data wasn't successfully saved due to quota exceed so throw an error
}
document.write(storage.getItem("name")); //Hello World!
storage.removeItem('name');
}
else {
alert('Your browser does not support HTML5 localStorage. Because Storage is' + storage);
}
}
but the problem is that the localStorage is always null, I have checked the permissions and created a WebChromeClient with
但问题是 localStorage 始终为空,我检查了权限并创建了一个 WebChromeClient
@Override
public void onExceededDatabaseQuota(String url,
String databaseIdentifier,
long currentQuota,
long estimatedSize,
long totalUsedQuota,
WebStorage.QuotaUpdater quotaUpdater)
{
quotaUpdater.updateQuota(estimatedSize * 2);
}
which never gets called. Anyone knows why?
永远不会被调用。有谁知道为什么?
Thanks
谢谢
回答by Maks
Have you tried just enabling the setting as per this answer : https://stackoverflow.com/a/5934650/85472
您是否尝试过按照此答案启用设置:https: //stackoverflow.com/a/5934650/85472