我正在试验 Android 代码:我想使用 HTML 5 本地存储来存储一个值。对于本练习,我使用一个像这样简单的页面:http://www.w3schools.com/html5/tryit.asp?filename=tryhtml5_webstorage_local_clickcount
我的清单确实允许我访问互联网,并且它的 min-sdk 为 7。
这是我的java代码:
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());
}
我的问题是,当我关闭应用程序时,本地存储的值不再存在。我可以使用默认浏览器浏览到同一页面,即使关闭模拟器后该值仍然是持久的,这正是我正在寻找的行为。
这可能是一件非常简单的事情......有什么想法吗?
看来空字符串 DatabasePath 是问题所在。我尝试了类似的代码,并且使用空字符串路径,该值在应用程序退出后不会保留。如果我定义特定的数据库路径,该值将按预期保留。
Try:
webSettings.setDatabasePath("/data/data/"+this.getPackageName()+"/databases/");
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)