LocalBrowser.java
package com.jamjalee.adn;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.JsResult;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class LocalBrowser extends Activity {
/** Called when the activity is first created. */
private WebView myWebView;
private static final String TAG="LocalBrowser";
private final Handler handler = new Handler();
private TextView textView;
private Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myWebView = (WebView)findViewById(R.id.web_View);
textView = (TextView)findViewById(R.id.text_view);
button = (Button)findViewById(R.id.button);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.addJavascriptInterface(new AndroidBridge(), "android");
myWebView.setWebViewClient(new WebViewClient(){
public boolean onJsAlert(final WebView view,
final String url, final String message, JsResult result){
Log.d(TAG, "onJsAlert("+view+", "+url+", "
+message+", "+result+")");
Toast.makeText(LocalBrowser.this, message, 3000).show();
result.confirm();
return true;
}
});
myWebView.loadUrl("file:///android_asset/index.html");
button.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
// TODO Auto-generated method stub
Log.d(TAG, "onClick("+view+")");
myWebView.loadUrl("javascript:callJS('Hello from Android')");
}
});
}
private class AndroidBridge{
public void callAndroid(final String arg){
handler.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Log.d(TAG, "callAndroid("+arg+")");
textView.setText(arg);
}
});
}
}
}//End of class
assets/index.html
<!DOCTYPE html>
<html>
<head>
<script language="JavaScript">
function callJS(arg){
document.getElementById('replaceme').innerHTML = arg;
}
</script>
</head>
<body>
<h2>WebView</h2>
<p>
<a href="#" onclick="window.alert('Alert from JavaScript')">
Display JavaScript alert</a>
</p>
<p>
<a href="#" onclick="window.android.callAndroid('Hello from Browser')">
Call Android from JavaScript</a>
</p>
<p id="replaceme">
</p>
</body>
</html>
res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<WebView
android:id="@+id/web_View"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"/>
<LinearLayout
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_weight="1.0"
android:padding="5sp">
<TextView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:textSize="24sp"
android:text="TextView"/>
<Button
android:id="@+id/button"
android:text="@string/call_javascript_from_android"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="18sp"/>
<TextView
android:id="@+id/text_view"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:textSize="18sp"/>
</LinearLayout>
</LinearLayout>
댓글 없음:
댓글 쓰기