在Android的WebView使用本地存储 [英] Using local storage on Android webview

查看:265
本文介绍了在Android的WebView使用本地存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与Android code的实验:我想用存储HTML 5本地存储的一个值。在这个练习中我使用这一个简单的页面:
http://www.w3schools.com/html5/tryit.asp?filename=tryhtml5_webstorage_local_clickcount

I am experimenting with Android code: I would like to store one value using HTML 5 local storage. For this exercise I' using a page as simple as this one: http://www.w3schools.com/html5/tryit.asp?filename=tryhtml5_webstorage_local_clickcount

我的表现确实让我打了互联网,这是7分钟-SDK。

My manifest does allow me to hit the internet, and it is min-sdk of 7.

下面是我的Java code:

Here is my java code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    webview = (WebView) findViewById(R.id.webview);
    WebView webView = (WebView) findViewById(R.id.webview);
    webView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
    WebSettings webSettings = webview.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setDatabasePath("");
    webSettings.setDomStorageEnabled(true);
    webview.setWebChromeClient(new WebChromeClient());
    webview.loadUrl("http://www.xyz.com/test.html");
    ///xyz.com/test.html is a sample  :)
    webview.setWebViewClient(new HelloWebViewClient());
}

我的问题是,当我关闭该应用程序,本地存储的值不再存在。我可以用默认的浏览器浏览到同一页面,甚至关闭模拟器,这正是我要寻找的行为后的值是永久性的。

My problem is that when I close the app, the locally stored value is no longer there. I can browse to the same page using the default browser, and the value is persistent even after closing the emulator, which is exactly the behavior that I am looking for.

这可能是非常简单的一个东西....任何想法?

This is probably a something extremely simple....any ideas?

推荐答案

这似乎空字符串DatabasePath就是问题所在。我试过类似code和一个空字符串的路径,该值不应用程序退出后仍然存在。如果我定义一个特定的数据库路径,则该值仍然存在预期。

It appears the empty string DatabasePath is the problem. I tried similar code and with an empty string path, the value does not persist after the app exits. If I define a specific database path, the value persists as expected.

尝试:

webSettings.setDatabasePath("/data/data/"+this.getPackageName()+"/databases/");

这篇关于在Android的WebView使用本地存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆