简体   繁体   English

如何从Android应用程序中托管的webView调用window.openDatabase?

[英]How do I call window.openDatabase from a webView hosted in an android application?

I created a native Android app that basically hosts a webView control. 我创建了一个原生Android应用程序,它基本上托管了一个webView控件。 That webView is then used to load a page that calls window.openDatabase from JavaScript. 然后,该webView用于加载从JavaScript调用window.openDatabase的页面。 I can successfully confirm that the API exists by verifying window.openDatabase. 我可以通过验证window.openDatabase来成功确认API是否存在。

Calling this method returns a null when called in a hosted webView control. 在托管的webView控件中调用时,调用此方法将返回null。 Calling the same method in the Android Browser returns an instance of the database. 在Android浏览器中调用相同的方法将返回数据库的实例。

Does anyone know the permessions in the manifest or settings on the webView control that need to be set to access the database? 有谁知道webView控件上的清单或设置中的渗透需要设置为访问数据库? Is this even possible in a native app. 这在本机应用程序中是否可行。

To use the database API you have to configure your WebView to accept HTML5 database calls: 要使用数据库API,您必须将WebView配置为接受HTML5数据库调用:

WebSettings settings = webView.getSettings();  
settings.setJavaScriptEnabled(true);  
settings.setJavaScriptCanOpenWindowsAutomatically(true); 
... 
settings.setDatabaseEnabled(true);  
settings.setDatabasePath("/data/data/your.package.name/database_name");

You may also override the onExceededDatabaseQuota method of your WebChromeClient if you want to control your storage quota: 如果要控制存储配额,还可以覆盖WebChromeClient的onExceededDatabaseQuota方法:

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

This solution worked for me: 这个解决方案对我有用:

WebSettings settings = myWebView.getSettings(); 
settings.setJavaScriptEnabled(true); 
settings.setDatabaseEnabled(true); 
String databasePath = this.getApplicationContext().getDir("database", 
                       Context.MODE_PRIVATE).getPath(); 
settings.setDatabasePath(databasePath); 

Can be also found here 也可以在这里找到

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android 4.0.3,window.openDatabase无法运作 - Android 4.0.3, window.openDatabase doesn't work window.openDatabase抛出TypeError:对象[object DOMWindow]在Android上没有方法'openDatabase' - window.openDatabase throws TypeError: Object[object DOMWindow] has no method 'openDatabase' on Android 是var db = window.openDatabase && openDatabase(); 避免错误的好方法? - Is var db = window.openDatabase && openDatabase(); a good way to avoid errors? 错误处理SQL:TypeError:window.openDatabase不是一个函数 - Error processing SQL: TypeError: window.openDatabase is not a function 是否可以在Adobe AIR中使用localStorage和window.openDatabase? - Is it possible to use localStorage and window.openDatabase in Adobe AIR? 如何在不使用webview的情况下从android phongap应用程序中的javascript调用本机android方法(函数)? - How to call native android method(function) from javascript in android phongap application, without using webview? openDatabase 在 Android PhoneGap 应用程序中创建错误 - openDatabase creates error in Android PhoneGap application 从commonJS模块调用.this时如何制作此窗口 - How do I make this window when .call(this) from a commonJS module 如何对 AWS 托管后端进行 HTTPS 调用 - How do I make a HTTPS call to AWS hosted backend 如何从应用程序的多个位置调用 clearInterval? - How do I call clearInterval from multiple places in my application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM