使用webview时,HTML localStorage在Android上为null

时间:2012-01-02 12:46:53

标签: javascript android webview local-storage

我正在尝试使用本地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);
            }

永远不会被召唤。 谁知道为什么?

由于

1 个答案:

答案 0 :(得分:11)

您是否尝试按照此答案启用该设置:https://stackoverflow.com/a/5934650/85472