使用HTML5开发Android本地应用(一)
来源:互联网 发布:php支持mysql扩展 编辑:程序博客网 时间:2024/06/04 17:55
HTML5是现在最热门的技术之一,Android自带的WebKit浏览器对于HTML5有着不错的支持
这一系列文章的目的在于研究和实验使用HTML5开发Android本地应用的优缺点和可行性,包括HT
首先是把一个WebView嵌入到应用界面中,建立起它和环境的交互,然后加载一个简单的HTML5页面。
String path = "file:///android_asset/index.html";String TAG = "WebClientDemo";boolean isLoadResources = true;WebView mWebView;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.webview1);init();mWebView.loadUrl(path);}
初始化WebView,设置参数,关键是设置语言为UTF-8和保证支持JavaScript
mWebView = (WebView) findViewById(R.id.myWebView1);mWebView.setBackgroundColor(Color.WHITE);mWebView.getSettings().setDefaultTextEncodingName("UTF-8");mWebView.getSettings().setSupportZoom(true);mWebView.getSettings().supportMultipleWindows();mWebView.getSettings().setJavaScriptEnabled(true);mWebView.clearView();
WebViewClient负责截获并修改加载网页过程中的各种事件。首先给mWebView设置一个新
mWebView.setWebViewClient(new WebViewClient() {// Intercepts url loadingpublic boolean shouldOverrideUrlLoading(WebView view, String url) {view.loadUrl(url);return true;} ... }
以下是在WebViewClient中,截获并且修改其它的事件行为的代码。例子代码中截获的事件包括网
// Intercepts the resource loading events@Overridepublic void onLoadResource(WebView view, String url) {if (!isLoadResources) {Log.i(TAG, "Block resource loading:" + url);return;} else {Log.i(TAG, "Continue resource loading:" + url);super.onLoadResource(view, url);}}// Intercepts error messagepublic void onReceivedError(WebView view, int errorCode,String description, String failingUrl) {Log.i(TAG, failingUrl);Toast.makeText(activity, "网页错误: " + errorCode + " 网页不可用",Toast.LENGTH_LONG).show();}// Intercepts form resubmissionpublic void onFormResubmission(WebView view, Message dontResend,Message resend) {Log.i(TAG, "Resubmission");Toast.makeText(activity, "不可重复提交,按Back回到上级网页",Toast.LENGTH_SHORT).show();}// Intercepts page started eventpublic void onPageStarted(WebView view, String url, Bitmap favicon) {Log.i(TAG, "Page load start");}// Intercepts page finished eventpublic void onPageFinished(WebView view, String url) {Log.i(TAG, "Page load finish");}
在Webview下,当按下Back键时,需要调用WebView中Back,访问历史页面,当没有历史页
需要在当前Activity中重写onKeyDown函数,处理Back事件。
public boolean onKeyDown(int keyCode, KeyEvent event) {// Forwards the back key event to browser pluginif ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {String url;mWebView.goBack();return true;}// Pops up a dialog before exitif ((keyCode == KeyEvent.KEYCODE_BACK) && (!mWebView.canGoBack())) {new AlertDialog.Builder(this).setTitle(R.string.title).setMessage(R.string.quit_desc).setNegativeButton(R.string.cancel,new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog,int which) {}}).setPositiveButton(R.string.confirm,new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog,int whichButton) {android.os.Process.killProcess(android.os.Process.myPid());}}).show();return true;}return super.onKeyDown(keyCode, event);}
WebChromeClient负责处理Javascript的对话框,网站图标,加载进度条等。下面的
mWebView.setWebChromeClient(new WebChromeClient() {public void onProgressChanged(WebView view, int progress) {activity.setProgress(progress * 100);}});
退出提示框
HTML5显示效果和页面代码:
<script type="application/x-javascript"> function drawText() { var canvas = document.getElementById("canvas"); if (canvas.getContext) { var ctx = canvas.getContext("2d"); var message = "First HTML5 page" ctx.fillStyle = "#FF0000"; ctx.font = "30px serif";var xPosition = 20;var yPosition = 30; ctx.fillText (message,xPosition,yPosition); } } </script> <body onload="drawText();"> <canvas id="canvas" width="300" height="300"></canvas> </body>
在以后的文章中,我们会讨论HTML5的其它功能和在手机上的应用方式。
转自:http://community-china.developer.motorola.com/t5/MOTODEV-%E5%8D%9A%E5%AE%A2/%E4%BD%BF%E7%94%A8HTML5%E5%BC%80%E5%8F%91Android%E6%9C%AC%E5%9C%B0%E5%BA%94%E7%94%A8-%E4%B8%80/ba-p/1742
- 使用HTML5开发Android本地应用(一)
- 使用HTML5开发Android本地应用(一)
- 使用HTML5开发Android本地应用
- 在Android中使用HTML5本地存储,地理位置,离线应用
- html5开发android应用
- Android开发HTML5应用
- 使用PhoneGap开发基于Html5应用一:PhoneGap简介
- HTML5本地存储——IndexedDB(一:基本使用)
- HTML5本地存储——IndexedDB(一:基本使用)
- HTML5本地存储——IndexedDB(一:基本使用)
- HTML5本地存储——IndexedDB(一:基本使用)
- HTML5本地存储——IndexedDB(一:基本使用)
- HTML5本地存储——IndexedDB(一:基本使用)
- HTML5本地存储——IndexedDB(一:基本使用)
- 使用HTML5实现本地数据存储(一)
- HTML5本地存储——IndexedDB(一:基本使用)
- HTML5本地数据库(一)
- 使用HTML5开发Android
- fedora16 更改默认登录的界面
- performSelector使用
- 项目管理模型总结---增量模型
- JBI规范
- Activity,Service,Broadcast与其自创建Handler的关系
- 使用HTML5开发Android本地应用(一)
- 读书笔记12:观察者模式
- shell脚本学习好书推荐
- 项目管理模型总结---原型模型、迭代模型
- 函数调用习惯与回调函数
- JBI简介
- hibernate数据库访问(翻译文档)
- 内存泄漏是新手必然要经历的痛,NSMutableArray的正确使用(转载)
- Android开发工具——ADB(Android Debug Bridge) <一>概览