一、离线应用缓存 HTML 5 Offline Application Cache
- 在服务器上添加MIME TYPE支:text/cache-manifest
如果在Apache下添加:
1 | AddType text/cache-manifest manifest |
如果为Nginx,在添加:
1 | text/cache-manifest manifest; |
或者通过动态程序生成:
1 | header( 'Content-type: text/cache-manifest; charset=UTF-8' ); |
- 创建 NAME.manifest:
新建清单文件 manifest
08 | /app/static/default/js/models/prototype.js |
09 | /app/static/default/js/controllers/init.js |
19 | /app/static/default/images/main/ bg .png |
- 给 <html> 标签加 manifest 属性:
建立manifest文件之后,需要在HTML文档中声明:
声明清单文件 manifest
<!doctype html>
<html manifest="notebook.manifest">
<head>
<meta charset="UTF-8" />
<meta name = "viewport" content = "width = device-width, user-scalable = no">
<title>NoteBook</title>
</head>
<body>
</body>
</html>
二、Key-Value Storage
三、Using the JavaScript Database
四、Android下使用WebView来做基于HTML5的App
见如下AndroidManifest.xml
01 | < ? xml version = "1.0" encoding = "utf-8" ?> |
03 | < application android:icon = "@drawable/icon" android:label = "@string/app_name" > |
04 | < activity android:name = ".Joke" android:label = "@string/app_name" android:configchanges = "orientation|keyboardHidden|navigation" android:theme = "@android:style/Theme.NoTitleBar" > |
06 | < action android:name = "android.intent.action.MAIN" > |
07 | < category android:name = "android.intent.category.LAUNCHER" > |
08 | </ category ></ action ></ intent > |
11 | < uses -permission = "" android:name = "android.permission.INTERNET" > |
注意:
1 | < uses -permission = "" android:name = "android.permission.INTERNET" ></ uses > |
, 允许网络应用,必须!!
Android主程序代码:
001 | package com.xinze.joke; |
003 | import android.app.Activity; |
004 | import android.app.AlertDialog; |
005 | import android.app.AlertDialog.Builder; |
007 | import android.content.DialogInterface; |
009 | import android.os.Bundle; |
010 | import android.view.KeyEvent; |
011 | import android.view.View; |
012 | import android.view.Window; |
013 | import android.view.WindowManager; |
014 | import android.webkit.JsPromptResult; |
015 | import android.webkit.JsResult; |
016 | import android.webkit.WebChromeClient; |
017 | import android.webkit.WebView; |
018 | import android.webkit.WebViewClient; |
020 | import android.webkit.WebStorage ; |
022 | public class Joke extends Activity { |
024 | /** Called when the activity is first created. */ |
026 | public void onCreate(Bundle savedInstanceState) { |
027 | super .onCreate(savedInstanceState); |
029 | final WebView wv = new WebView( this ); |
032 | wv.setOnKeyListener( new View.OnKeyListener() { |
035 | public boolean onKey(View v, int keyCode, KeyEvent event) { |
036 | if (event.getAction() == KeyEvent.ACTION_DOWN) { |
037 | if ((keyCode == KeyEvent.KEYCODE_BACK) && wv.canGoBack()) { |
047 | wv.getSettings().setJavaScriptEnabled( true ); |
049 | wv.getSettings().setJavaScriptCanOpenWindowsAutomatically( true ); |
050 | wv.getSettings().setDatabaseEnabled( true ); |
051 | wv.getSettings().setDatabasePath( "/data/data/com.xinze.joke/databases" ); |
054 | WebViewClient wvc = new WebViewClient() { |
057 | public boolean shouldOverrideUrlLoading(WebView view, String url) { |
065 | wv.setWebViewClient(wvc); |
068 | WebChromeClient wvcc = new WebChromeClient() { |
072 | public boolean onJsAlert(WebView view, String url, String message, final JsResult result) { |
074 | Builder builder = new Builder(Joke. this ); |
075 | builder.setTitle( "笑死不偿命" ); |
076 | builder.setMessage(message); |
077 | builder.setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener() { |
079 | public void onClick(DialogInterface dialog, int which) { |
083 | builder.setCancelable( false ); |
091 | public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) { |
092 | Builder builder = new Builder(Joke. this ); |
093 | builder.setTitle( "删除确认" ); |
094 | builder.setMessage(message); |
095 | builder.setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener() { |
098 | public void onClick(DialogInterface dialog, int which) { |
103 | builder.setNeutralButton(android.R.string.cancel, new AlertDialog.OnClickListener() { |
106 | public void onClick(DialogInterface dialog, int which) { |
111 | builder.setCancelable( false ); |
119 | public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, |
120 | JsPromptResult result) { |
122 | return super .onJsPrompt(view, url, message, defaultValue, result); |
126 | public void onExceededDatabaseQuota(String url, String |
127 | databaseIdentifier, long currentQuota, long estimatedSize, long |
128 | totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) { |
129 | quotaUpdater.updateQuota( 204801 ); |
137 | wv.setWebChromeClient(wvcc); |
使用 JavaScript Database 的时候,需要特别注意:setDatabaseEnabled 以及 onExceededDatabaseQuota!