我试图从我的本地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);
} }
这是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);
}
}
但问题是localStorage总是为null,我检查了权限并创建了一个WebChromeClient
@Override
public void onExceededDatabaseQuota(String url,String databaseIdentifier,long currentQuota,long estimatedSize,long totalUsedQuota,WebStorage.QuotaUpdater quotaUpdater)
{
quotaUpdater.updateQuota(estimatedSize * 2);
}
从来没有被称为.
谁知道为什么?
谢谢
解决方法
您是否尝试过根据此答案启用设置:
https://stackoverflow.com/a/5934650/85472