¾È±Ô °øºÎ¹æ

Android > Webview - JavaScriptMethods() - À¥ºä¿Í ÀÚ¹Ù½ºÅ©¸³Æ®

µî·ÏÀÏ : 2017-07-06 17:45 Á¶È¸¼ö : 53,464

¸ÕÀú WebViewÀÇ ¸¸µå´Â ¹æ¹ýÀ» ¾Ë¾Æ ºÃ´Ù.

À¥ºäÀÇ ¼³Á¤Á¤º¸´Â WebSettings °´Ã¼¿¡ Æ÷ÇԵǾî Àִµ¥, ÀÌ ¼ÂÆà Á¤º¸°¡ ¿©·¯°¡Áö°¡ Àִµ¥¿ä.

ÀÌ Áß¿¡¼­ ÀÚ¹Ù½ºÅ©¸³Æ®¸¦ Çã¿ëÇϱâ À§Çؼ­´Â setJavaScriptEnabled() ¸ð¼­µå¸¦ ÀÌ¿ëÇϴµ¥, setJavaScriptEnabled(true) ·Î ¼³Á¤ÇØ¾ß ÇÕ´Ï´Ù.

½ºÅ©¸³Æ® Çã¿ë ¿©ºÎ´Â µðÆúÆ®°¡ 'false'·Î µÇ¾î ÀÖÀ¸¹Ç·Î ÀÚ¹Ù½ºÅ©¸³Æ®¸¦ »ç¿ëÇØ¾ß ÇÒ ÇÊ¿ä°¡ ÀÖÀ» ¶§´Â ÀÌ Äڵ带 Ãß°¡ÇØ¾ß ÇÕ´Ï´Ù.
 
	private WebView webView;
	private Handler mHandler = new Handler();

	public void Connect_URL(String conn_url)
	{ 
		String conn_url = "http://abc.co.kr";

		webView = (WebView) findViewById(R.id.webview1);             

		WebSettings webSettings = webView.getSettings(); 
		webSettings.setSavePassword(false); 
		webSettings.setSaveFormData(false); 
		webSettings.setJavaScriptEnabled(true); 
		webSettings.setSupportZoom(false);      
		webView.setWebChromeClient(new WebBrowserClient());
		webView.setWebViewClient(new InfoWebViewClient());            

		webView.addJavascriptInterface(new JavaScriptMethods(), "sample");     

		webView.loadUrl(conn_url);
	}

  
	final class JavaScriptMethods {  
		JavaScriptMethods() { }  
        
		//À¥ÆäÀÌÁöÀÇ ÀÚ¹Ù½ºÅ©¸³Æ®´Â ¿©±â¿¡¼­ ó¸®ÇÑ´Ù.
		public void fnc_test()
		{   
			Toast.makeText(getApplicationContext(),"This Function fnc_test()",500).show();
		}

		//¾ÖÇø®ÄÉÀ̼ǿ¡¼­ À¥ÆäÀÌÁöÀÇ ÀÚ¹Ù½ºÅ©¸³Æ®¸¦ È£ÃâÇÏ´Â ¿¹Á¦
		public void clickToPage()
		{
			mHandler.post(new Runnable() {
				public void run() {
					webView.loadUrl("javascript:changeFace()");
				}
			});
		}      
    } 
	


À§ÀÇ ¼Ò½º¿¡¼­ À¥ÆäÀÌÁöÀÇ addJavascriptInterface() ¸Þ¼­µå¸¦ ÅëÇØ À¥ÆäÀÌÁöÀÇ ÀÚ¹Ù½ºÅ©¸³Æ® ÀÎÅÍÆäÀ̽º ÇÔ¼ö¸¦ ¼³Á¤ÇÒ ¼ö Àִµ¥,

JavaScriptMethods Ŭ·¹½º¿¡¼­ ¹Þ¾Æ¼­ ó¸® ÇÒ ¼ö ÀÖ½À´Ï´Ù.

"sample" Àº °´Ã¼À̸§ÀÌ µË´Ï´Ù.

Áï À¥ÆäÀÌÁö¿¡¼­ ÀÚ¹ÙÀÇ fnc_test() ÇÔ¼ö¸¦ È£ÃâÇÏ°íÀÚ ÇÒ¶§´Â window.sample.fnc_test() ó·³ »ç¿ë ÇÒ ¼ö ÀÖ½À´Ï´Ù.


¾ÖÇø®ÄÉÀ̼ǿ¡¼­ À¥ÆäÀÌÁöÀÇ ÀÚ¹Ù½ºÅ©¸³Æ®¸¦ È£ÃâÇÏ´Â ¿¹Á¦´Â clickToPage() ÇÔ¼ö ÀÔ´Ï´Ù.

¿©±â¿¡¼­´Â ÀÚ¹Ù½ºÅ©¸³Æ®¸¦ ÀÌ¿ëÇØ Á¢±ÙÇÒ ¶§ ¾Èµå·ÎÀ̵忡¼­ Á¤ÀÇµÈ Çڵ鷯¸¦ »ç¿ëÇØ¾ß Çϸç, Runnable °´Ã¼¸¦ ¸¸µé°í

post() ¸Þ¼­µå¸¦ ÀÌ¿ëÇÕ´Ï´Ù.


 
¡Ø Ȥ½Ã µµ¿òÀÌ µÇ¼Ì´Ù¸é ´ñ±Û¿¡ ÇѸ¶µð ³²°ÜÁÖ¼¼¿ä!
ÀÛ¼ºÀÚ   ºñ¹Ð¹øÈ£
ÀÚµ¿±Û ¹æÁö     (ÀÚµ¿±Û ¹æÁö ±â´ÉÀÔ´Ï´Ù.)
³»¿ë   ´ñ±Û´Þ±â 
À̸ÞÀÏ ¹®ÀÇ : cak0280@nate.com  
Copyright 2000 By ENTERSOFT.KR All Rights Reserved.