Android Webview和LocalStorage

我的webview有问题,它可能通过HTML5应用程序访问localStorage 。test.html文件通知我浏览器(即webview)不支持本地存储。如果您有任何建议。


package com.test.HelloWebView; 

import android.app.Activity; 

import android.content.Context; 

import android.os.Bundle; 

import android.util.Log; 

import android.view.KeyEvent; 

import android.webkit.WebChromeClient; 

import android.webkit.WebSettings; 

import android.webkit.WebStorage; 

import android.webkit.WebView; 

import android.webkit.WebViewClient; 

public class HelloWebView extends Activity { 

WebView webview; 

public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 

    setContentView(R.layout.main); 

    webview = (WebView) findViewById(R.id.webview); 

    webview.getSettings().setJavaScriptEnabled(true); 

    webview.setWebViewClient(new HelloWebViewClient()); 

    webview.loadUrl("file:///android_asset/test.html"); 

    WebSettings settings = webview.getSettings(); 

    settings.setJavaScriptEnabled(true); 

    settings.setDatabaseEnabled(true); 

    String databasePath = this.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath(); 

    settings.setDatabasePath(databasePath);

    webview.setWebChromeClient(new WebChromeClient() { 

    public void onExceededDatabaseQuota(String url, String databaseIdentifier, long currentQuota, long estimatedSize, long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) { 

            quotaUpdater.updateQuota(5 * 1024 * 1024); 

        } 

    }); 

public boolean onKeyDown(int keyCode, KeyEvent event) { 

    if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) { 

        webview.goBack(); 

        return true; 

    } 

    return super.onKeyDown(keyCode, event); 

private class HelloWebViewClient extends WebViewClient { 

    public boolean shouldOverrideUrlLoading(WebView view, String url) { 

        view.loadUrl(url); 

        return true; 

    } 

}


梵蒂冈之花
浏览 1085回答 3
3回答

慕哥6287543

缺少以下内容:settings.setDomStorageEnabled(true);

PIPIONE

在API级别19中已弃用setDatabasePath()方法。建议您使用如下存储区域设置:webView.getSettings().setDomStorageEnabled(true);webView.getSettings().setDatabaseEnabled(true);if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {&nbsp; &nbsp; webView.getSettings().setDatabasePath("/data/data/" + webView.getContext().getPackageName() + "/databases/");}

万千封印

适用于我的Android 4.2.2的解决方案,已使用构建目标Android 4.4W进行了编译:WebSettings settings = webView.getSettings();settings.setDomStorageEnabled(true);settings.setDatabaseEnabled(true);if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {&nbsp; &nbsp; File databasePath = getDatabasePath("yourDbName");&nbsp; &nbsp; settings.setDatabasePath(databasePath.getPath());}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5
Android