Android
webpage interface
iRang1101
2014. 12. 22. 17:41
webView를 사용하여 화면을 보여줄때...
wb과 app과의 인터페이스가 필요할때가 있었다.
1. web페이지에서 app을 호출할 경우는 다음과 같다.
step 1 : 웹페이지
function reqMsg(arg){ window.AndroidFunction.sendMessage(arg); } |
step 2 : app에서의 activity
_wvNews = (WebView)findViewById(R.id.webView); _wvNews.getSettings().setJavaScriptEnabled(true); _wvNews.getSettings().setLoadsImagesAutomatically(true); _wvNews.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE); _wvNews.getSettings().setLoadWithOverviewMode(true); _wvNews.setWebViewClient(new WebViewClientClass()); _wvNews.addJavascriptInterface(new WebAppInterface(this), "AndroidFunction"); _wvNews.loadUrl("http://www.test.co.kr/~testUser/test.html"); public class WebAppInterface { Context mContext; /** Instantiate the interface and set the context */ WebAppInterface(Context c) { mContext = c; } /** Show a toast from the web page */ @JavascriptInterface public void sendMessage(final String msg) { MainActivity.this.runOnUiThread(new Runnable() { public void run() { Log.e(TAG, "msg= " + msg); } }); } } |
2. app에서 webpage의 javascript 호출 시...
step 1 : 웹페이지
function recvAndroidMsg(arg){ document.getElementById("title").innerHTML = arg; }
|
step 2 : app에서의 activity
_wvNews.loadUrl("javascript:recvAndroidMsg('"+purchase.getSku()+"')"); |