Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
135 views
in Technique[技术] by (71.8m points)

javascript - 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. That webView is then used to load a page that calls window.openDatabase from JavaScript. I can successfully confirm that the API exists by verifying window.openDatabase.

Calling this method returns a null when called in a hosted webView control. Calling the same method in the Android Browser returns an instance of the database.

Does anyone know the permessions in the manifest or settings on the webView control that need to be set to access the database? Is this even possible in a native app.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To use the database API you have to configure your WebView to accept HTML5 database calls:

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:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...