This post consists of: http://www.sollyu.com/android-software-development-webview-addjavascriptinterface-cycle-of-gradual-two/
Description article List
Android software development Webview.addjavascriptinterface cycle progressive "one": http://www.sollyu.com/?p=302
Android software development Webview.addjavascriptinterface Cycle progressive "two": http://www.sollyu.com/?p=586
After the release of the previous article, we have been unanimously praised, in the article promised in the second article has been not come out, today to fill out. OK, we start directly, because the previous article wrote the time is too early, there is no way to restore the original project, so I have a new project here.
New Project
In order to make it easier for everyone to understand, so I will try to shorten the code here, this is the configuration of my project
After you have created the project, then adjust the interface to the following
Add code
First add two buttons and WebView, and add two to the Web JavaScript调用接口 , testFunc1 andtestFunc2
Private Button m_testbuttom1;Private Button M_testbuttom2;Private WebView M_webview;@Suppresslint("Setjavascriptenabled")@Overrideprotected void onCreate(Bundle savedinstancestate) { Super.onCreate(savedinstancestate); Setcontentview(R.Layout.Activity_main); m_testbuttom1 = (Button)Findviewbyid(R.ID.button1); M_testbuttom2 = (Button)Findviewbyid(R.ID.Button2); M_webview = (WebView)Findviewbyid(R.ID.WebView1); M_webview.getsettings().setjavascriptenabled(true); M_webview.Addjavascriptinterface(New Object(){ @suppresswarnings("Unused") // These two functions can beJavaScriptcalled in Public void testFunc1(String string) {MessageHandler.Obtainmessage(MessageHandler.wm_set_func1, string).Sendtotarget(); } @suppresswarnings("Unused") Public void TESTFUNC2(String string) { MessageHandler.Obtainmessage(MessageHandler.WM_SET_FUNC2, string).Sendtotarget();} }, "Demo"); M_webview.Loadurl("File:///android_asset/demo.html"); }
You can see that there's one in there messageHandler , and we'll look at his specific definition, which is primarily for JavaScript interaction, and JavaScript uses this message to interact with our UI, as in the previous article.
Looper Looper = Looper.Mylooper(); //gets the current thread'sLooperinstance, because the current thread isUIthreads can also be passedLooper.Getmainlooper()GetPrivate Handler MessageHandler = New MessageHandler(Looper);//There is even no need to setLooper, because HandlerThe default is to use the current thread'sLooper Public class MessageHandler extends Handler { Public Static Final int wm_set_func1 = 0; Public Static Final int WM_SET_FUNC2 = 1; Public MessageHandler(Looper Looper) { Super(Looper); } @Override Public void Handlemessage(Message msg) { Switch (msg. What) { Case wm_set_func1: Toast.Maketext(Getapplicationcontext(), msg.obj.toString(), Toast.Length_long).Show(); Break; Case WM_SET_FUNC2: Toast.Maketext(Getapplicationcontext(), msg.obj.toString(), Toast.Length_long).Show(); Break; Default: Break; } }}
After adding the button click event in order to add
m_testbuttom1.Setonclicklistener(New Onclicklistener() { @Override Public void OnClick(View arg0) { String string = "Http://www.sollyu.com"; M_webview.Loadurl("Javascript:testfunc1 ('"+string+"');"); // calledHTMLin theJavaScriptfunctiontestFunc1,there's only one parameter here . }});M_testbuttom2.Setonclicklistener(New Onclicklistener() { @Override Public void OnClick(View arg0) { String string = "Here are two parameters:"; int nInt = 191067617; M_webview.Loadurl("Javascript:testfunc2 ('"+string+"',"+String.valueOf(nInt)+")"); // GM here has2a parameter }});
So after the Android code is finished, here's a look atdemo.html
HTML file
demo.htmlThe source code is:
<script language= "JavaScript" > function TestFunc1 (var1) { Return_var = "Original article:" + var1; WINDOW.DEMO.TESTFUNC1 (Return_var); Call the TESTFUNC1 function in Android code return return_var; } function TestFunc2 (var1,var2) { Return_var = var1 + var2; WINDOW.DEMO.TESTFUNC2 (Return_var); The parameter here is return to Android value return Return_var; } </script>
Run
Android software development Webview.addjavascriptinterface Cycle progressive "two"