i am work at an app which Embed an WebView and display a HTML5 page .
when open the html page in android chrome Browser.the database was create normal.
but when i run this app , the database can not create . it seem that html page can not create the database on WebView.
anybody know why?
here is my Activity code:
public class efan_NewsReader extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView myWebView=(WebView)findViewById(R.id.my_webview);
myWebView.setWebViewClient(new WebViewClient());
WebSettings settings = myWebView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setDomStorageEnabled(true);
settings.setDatabaseEnabled(true);
settings.setAppCacheEnabled(false);
myWebView.loadUrl("http://10.10.35.47:8080/html5test/test.htm");
}}
here is my HTML5 page source code:
<html manifest="mymanife开发者_StackOverflow社区st.manifest">
<head>
<meta http-equiv="Content-Type" content="text/html; content="no-cache" charset=utf-8" />
<script type="text/javascript" src="js/jquery-1.6.1.min.js"></script>
<script>
$(document).ready(function(){
databaseTest();
});
function databaseTest(){
//open database
var db = openDatabase('mydb', '1.0', 'Test DB', 2 * 1024 * 1024);
db.transaction(function (tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS testHtml (id unique, contentText)');
tx.executeSql('INSERT INTO testHtml (contentText) VALUES ("insert data test!")');
});
db.transaction(function(tx){
tx.executeSql('SELECT * FROM testHtml',[],function(tx,result){
var len=result.rows.length;
var msg = "<p>Found rows: " + len + "</p>";
$("#testinfo").append(msg);
},null);
});
}
</script>
</head>
<body>
<div>here is test info:</div>
<div id="testinfo"></div>
</body>
Try :
settings.setDatabaseEnabled(true);
String dbPath = this.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
settings.setDatabasePath(dbPath);
And in WebChromeClient :
myWebView.setWebChromeClient(new WebChromeClient()
{
@Override
public void onExceededDatabaseQuota(String url, String databaseIdentifier, long currentQuota, long estimatedSize, long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater)
{
quotaUpdater.updateQuota(estimatedSize * 2);
}
}
It should be OK ;)
new WebChromeClient() was throwing a constructor error so I did it this way. private WebView webView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_web);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setDatabaseEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
//String sDataPath = "/data/data/" + webView.getContext().getPackageName() + "/databases/";
String sDataPath = this.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
webView.getSettings().setDatabasePath(sDataPath);
webView.getSettings().setDatabaseEnabled(true);
webView.getSettings().setSavePassword(true);
webView.getSettings().setAllowContentAccess(true);
webView.getSettings().setAllowFileAccess(true);
webView.setWebViewClient(new Callback());
webView.setWebChromeClient(new webChromeClient());
webView.loadUrl("http://www.yahoo.com");
}
private class Callback extends WebViewClient{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return (false);
}
}
private class webChromeClient extends WebChromeClient
{
@Override
public void onExceededDatabaseQuota(String url, String databaseIdentifier, long currentQuota, long estimatedSize, long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
quotaUpdater.updateQuota(5 * 1024 * 1024);
}
}
It's likely that the default quota is 0, so onExceededDatabaseQuota
is a must
精彩评论