mWebView = (WebView) this.findViewById(R.id.webview);
WebSettings settings = mWebView.getSettings();
settings.setJavaScriptEnabled(true);
//settings.setPluginsEnabled(true);
/***打开本地缓存提供JS调用**/
mWebView.getSettings().setDomStorageEnabled(true);
// Set cache size to 8 mb by default. should be more than enough
mWebView.getSettings().setAppCacheMaxSize(1024*1024*8);
// This next one is crazy. It's the DEFAULT location for your app's cache
// But it didn't work for me without this line.
// UPDATE: no hardcoded path. Thanks to Kevin Hawkins
String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();
mWebView.getSettings().setAppCachePath(appCachePath);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.getSettings().setAppCacheEnabled(true);
java的navtive代码只要设置了以上参数,就可以为JS端提供本地存储了,记住这个参数需要API>=7使用,也就是android2.1版本以上才可以。
本文深入解析了如何在Java中通过WebView组件为JavaScript端提供本地存储功能,包括设置JavaScript启用、DOM存储、应用缓存大小、路径以及文件访问权限等关键参数。确保Android应用2.1版本以上实现高效本地缓存与JS交互。
708

被折叠的 条评论
为什么被折叠?



