如何在WebView中建立Android Apps
1. Web Apps的两种形式
在Android中,Web Apps有两种形式供用户访问。一种就是用手机上的浏览器直接访问的网络应用程序,这种情况用户不需要额外安装其他应用,只要有浏览器就行;而另一种,则是在用户的手机上安装客户端应用程序(.apk),并在此客户端程序中嵌入Web View来显示从服务器端下载下来的网页数据,比如新浪微博和人人网的客户端。对于前者来说,主要的工作是根据手机客户端的屏幕来调整网页的显示尺寸、比例等;而后者需要单独开发基于Web View的Web app. 本篇主要是学习后者的开发。
2. 怎样在Android应用程序中加入Web View?
2.1 先在layout文件中加入<WebView>元素
<WebView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/webview" android:layout_width="fill_parent" android:layout_height="fill_parent"/>
<uses-permission android:name="android.permission.INTERNET"/>
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("http://www.xxx.com");
WebSettings webSettings = myWebView.getSettings(); // 开启Web View对JavaScript的支持 webSettings.setJavaScriptEnabled(true);
public class JavaScriptInterface { Context mContext; /** 初始化context,供makeText方法中的参数来使用 */ JavaScriptInterface(Context c) { mContext = c; }
public void showToast(String toast) { Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show(); } }
webView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
<script type="text/javascript"> function showAndroidToast(toast) { Android.showToast(toast); } </script> <input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />
myWebView.setWebViewClient(new WebViewClient());
private class MyWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { if (Uri.parse(url).getHost().equals("www.example.com")) { // This is my web site, so do not override; let my WebView load the page return false; } // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); startActivity(intent); return true; } }
@Override public boolean onKeyDown(int keyCode, KeyEvent event) { // Check if the key event was the BACK key and if there's history if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack() { myWebView.goBack(); return true; } // If it wasn't the BACK key or there's no web page history, bubble up to the default // system behavior (probably exit the activity) return super.onKeyDown(keyCode, event); }
package com.WebApp; import android.app.Activity; import android.os.Bundle; import android.view.KeyEvent; import android.webkit.WebSettings; import android.webkit.WebView; public class Home extends Activity { // declare a WebView private WebView myWebView; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); // initialize the WebView myWebView = (WebView) findViewById(R.id.webview); /* Enable the JavaScript in Web View */ WebSettings webSettings = myWebView.getSettings(); webSettings.setJavaScriptEnabled(true); // bind the Android code to JavaScript code myWebView.addJavascriptInterface(new myJavaScriptInterface(), "myJS"); // load a web page myWebView.loadUrl("file:///android_asset/first.html"); } /** * This class is an interface between Android and JavaScript * whose methods can be accessed by JavaScript code */ final class myJavaScriptInterface { myJavaScriptInterface() { } /** * load the content page */ public void LoadContentPage() { myWebView.loadUrl("file:///android_asset/second.html"); } } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // Check if the key event was the BACK key and if there's history if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()){ myWebView.goBack(); return true; } // If it wasn't the BACK key or there's no web page history, bubble up to the default // system behavior (probably exit the activity) return super.onKeyDown(keyCode, event); } }
<html> <body> <!-- 调用Android代码中的方法 --> <a onClick="window.myJS.LoadContentPage()" style="text-decoration: underline"> Google+ is now under testing! </a> </body> </html>