標籤:html5 緩衝 layout
因為需要,我要在andriod手機上用webview給我的HTML5項目加一個殼,但第一次將項目封裝進andriod裡面時,我測試輸出的alert盡然是不支援HTML5 web緩衝,這讓我很鬱悶,在網上找了了一些資料看了一下,測試了幾次終於通了,因為我不是搞andriod的,所以就不詳細介紹了,把代碼分享給大家:
MainActivity.java類:
package com.example.test; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.util.Log; import android.view.KeyEvent; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebStorage; import android.webkit.WebView; import android.webkit.WebViewClient; public class MainActivity extends Activity { WebView webview; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); webview = (WebView) findViewById(R.id.webView1); webview.getSettings().setJavaScriptEnabled(true); webview.setWebViewClient(new HelloWebViewClient()); webview.loadUrl("file:///android_asset/indexfuza.html"); WebSettings settings = webview.getSettings(); settings.setJavaScriptEnabled(true); settings.setDatabaseEnabled(true); String databasePath = this.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath(); settings.setDatabasePath(databasePath); WebSettings webseting = webview.getSettings(); webseting.setDomStorageEnabled(true); webseting.setAppCacheMaxSize(1024*1024*8);//設定緩衝大小,我設的是8M String appCacheDir = this.getApplicationContext().getDir("cache", Context.MODE_PRIVATE).getPath(); webseting.setAppCachePath(appCacheDir); webseting.setAllowFileAccess(true); webseting.setAppCacheEnabled(true); webseting.setCacheMode(WebSettings.LOAD_DEFAULT); webview.setWebChromeClient(new WebChromeClient() { public void onExceededDatabaseQuota(String url, String databaseIdentifier, long currentQuota, long estimatedSize, long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) { quotaUpdater.updateQuota(5 * 1024 * 1024); } }); } public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) { webview.goBack(); return true; } return super.onKeyDown(keyCode, event); } private class HelloWebViewClient extends WebViewClient { public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } }} layout裡面的activity_main.xml檔案:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <WebView android:id="@+id/webView1" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
其他的地方我也沒動,就這樣就支援HTML5 web儲存啦。。。。。。。。
我的項目10:Android的webview支援HTML5的離線應用功能詳細配置