我的項目10:Android的webview支援HTML5的離線應用功能詳細配置

來源:互聯網
上載者:User

標籤: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的離線應用功能詳細配置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.