안드로이드에서 WebView, WebChromeClient를 이용하여 웹페이지를 보여주는 예
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.androidapp" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> <uses-permission android:name="android.permission.INTERNET"/> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:configChanges="orientation" android:label="@string/title_activity_main" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
res/layout/main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rootLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <WebView android:id="@+id/webView1" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
http://211.183.2.80:8080/WebApp/hello.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=EUC-KR"> <title>안녕하세요?</title> <script type="text/javascript"> function test() { var name = "Smith"; alert('안녕하세요? ' + name); } function changeImage() { document.getElementById("mountain").src = 'images/mt05.jpg'; } </script> </head> <body> <div align="center"> 안녕하세요?<p/> <input type="button" value="alert() 함수 실행" onClick="javascript:test(); "/> <p/> <img id="mountain" src="images/mt06.jpg" /><p/> <input type="button" value="이미지 변경" onClick="changeImage();" /> </div> </body> </html>
MainActivity.java
package com.example.androidapp; import android.os.Bundle; import android.app.*; import android.content.DialogInterface; import android.util.Log; import android.webkit.JsResult; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; public class MainActivity extends Activity { WebView webView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Log.i("onCreate", "onCreate실행됨"); webView = (WebView) findViewById(R.id.webView1); WebSettings webSettings = webView.getSettings(); webSettings.setSavePassword(false); webSettings.setSaveFormData(false); webSettings.setJavaScriptEnabled(true); webSettings.setSupportZoom(false); //webView.loadUrl("file:///android_asset/testhtml.html"); webView.loadUrl("http://211.183.2.80:8080/WebApp/hello.jsp"); webView.setWebChromeClient(new MyChromeClient()); } /* Javascript의 onCloseWindow, onProgressChanged, onJsAlert, onJsConfirm, * onJsPrompt, onJsTimeOut 등의 이벤트처리를 담당하는 클래스 */ class MyChromeClient extends WebChromeClient { @Override public boolean onJsAlert(WebView view, String url, String message,final JsResult result) { /*Javascript에서 alert()함수가 사용될 때 자동으로 호출되는 콜백 메소드 * 안드로이드의 AlertDialog를 작성하여 보여주면 된다 */ new AlertDialog.Builder(MainActivity.this) .setTitle("JavaScript Alert !") .setMessage(message) .setPositiveButton(android.R.string.ok,/*대신 CharSequence도 가능*/ new AlertDialog.OnClickListener() { public void onClick(DialogInterface dialog, int which) { result.confirm(); } }).create().show(); return true; } } }