[Android]app <---> javascript 통신
개발/Javascript2011. 4. 18. 14:27
AndroidMainifest.xml
<uses-permission android:name="android.permission.INTERNET" />
main.xml
<EditText android:id="@+id/input_area"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button android:id="@+id/submitBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="submit"
/>
<WebView android:id="@+id/webview" android:layout_width="fill_parent" android:layout_height="fill_parent" />
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button android:id="@+id/submitBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="submit"
/>
<WebView android:id="@+id/webview" android:layout_width="fill_parent" android:layout_height="fill_parent" />
java code
private final Handler handler = new Handler();
private static final String JAVASCRIPT_APP_KEY = "WebviewExample";
private TextView lableView = null;
private Button submitBtn = null;
lableView = (TextView)findViewById(R.id.labelView);
final WebView wv = (WebView)findViewById(R.id.webview);
wv.getSettings().setJavaScriptEnabled(true);
wv.addJavascriptInterface(new AndroidBridge(), JAVASCRIPT_APP_KEY);
wv.loadUrl("url");
//버튼이 눌려질 경우
submitBtn = (Button)findViewById(R.id.submitBtn);
submitBtn.setOnClickListener(new View.OnClickListener(){
EditText txt = (EditText)findViewById(R.id.input_area);
@Override
public void onClick(View v) {
wv.loadUrl("javascript:callJavascript('"+txt.getText()+"')");
}
});
private class AndroidBridge {
public void callAndroid(final String arg){
handler.post(new Runnable() {
@Override
public void run() {
Log.i(TAG, "setMessage("+arg+")");
lableView.setText(arg);
}
});
}
}
private static final String JAVASCRIPT_APP_KEY = "WebviewExample";
private TextView lableView = null;
private Button submitBtn = null;
lableView = (TextView)findViewById(R.id.labelView);
final WebView wv = (WebView)findViewById(R.id.webview);
wv.getSettings().setJavaScriptEnabled(true);
wv.addJavascriptInterface(new AndroidBridge(), JAVASCRIPT_APP_KEY);
wv.loadUrl("url");
//버튼이 눌려질 경우
submitBtn = (Button)findViewById(R.id.submitBtn);
submitBtn.setOnClickListener(new View.OnClickListener(){
EditText txt = (EditText)findViewById(R.id.input_area);
@Override
public void onClick(View v) {
wv.loadUrl("javascript:callJavascript('"+txt.getText()+"')");
}
});
private class AndroidBridge {
public void callAndroid(final String arg){
handler.post(new Runnable() {
@Override
public void run() {
Log.i(TAG, "setMessage("+arg+")");
lableView.setText(arg);
}
});
}
}
html
<script type="text/javascript">
function callAndroid(){
window.WebviewExample.callAndroid("android call");
document.getElementById('area').innerHTML = "android call";
}
function callJavascript(message){
document.getElementById('area').innerHTML = message;
}
</script>
<a href="#" onclick="callAndroid()">call</a><br/>
<div id="area"></div>
'개발 > Javascript' 카테고리의 다른 글
javascript grap library (0) | 2014.03.06 |
---|---|
HTML 우클릭 금지와 드레그 금지 (0) | 2013.11.05 |
[jquery] ajax 사용하기 (0) | 2010.12.17 |
javascript 정리 (0) | 2010.12.17 |
[jquery] JQuery Ajax 사용하기 (0) | 2010.07.23 |