This post consists of: http://www.sollyu.com/android-software-development-webview-addjavascriptinterface-cycle-of-gradual-one/
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
The first necessary wordy a few words, these days write VC write tired of dying, suddenly don't want to write about VC, hand inside there is a andriod mobile phone every day to play to midnight, but never wrote this aspect of the program, really is sad ah. So I would like to write this aspect of the program, with the VC of the habit of learning procedures, the first program is of course Hello world. At first, there is no big problem, when you want to write a brush q* members of the program, but found that the Android interface layout is really difficult ah, how not to look good. To make a mess with .... anything. The head of the gas is big, think oneself wrote so long time VC really did not encounter such interface problem, really can be said.
When I saw the video of Daniel on the VERYCD, I heard someone say that I could use HTML to make an Android UI interface, which not only made my eyes bright. :-) Because I learned HTML development, although learning is not good, but it is better than this XML is too much, really is the big XML head ... So the beginning of the Andriod HTML interface but later found in this aspect of the article that called a little pity Ah, the foreigner there is not to understand, or the basic and I see the content irrelevant. Because he is a rookie, so I think to find a group, rub in to find a Daniel Ask, who knows in, I xxx His, incredibly charge, I xx its oo, others Android are open source ask you a broken question also charge, what morals ah. At that time an atmosphere,,, vowed to learn, write an article online, let you charge, xx you oo.
It's a lot more comfortable to vent,:-). OK, I'm not going to say much. Let's start with our Android WebView HTML interface tutorial. Here I give you a domestic Daniel HTML interface video tutorial, but he said I did not understand.
21.android UI design tips. rar or watch 3G mobile Android app development Eighth day course 1. Use HTML design software interface. avi His old video also wants [Android Dev video tutorial]02_12_ JSON data parsing. mp4 This video to do the basics. If you are also Daniel, you can certainly read it. The following is a tutorial on the development of our vegetable class.
21.android UI design tips. rar
3G Mobile Android App development Eighth day course 1. html design software interface. avi
Android Development Video Tutorial 02_12_json Data solution
Code
Since this is the first stage of the WebView, I will write the simplest article, so that everyone can cycle gradually to understand addJavascriptInterface how this sentence is used.
Here we cite a case of Google the following is a dome I Google to provide the address: Click to open the link
We first look at its HTML file, the HTML file path is in: Click to open the link
His code is:
<script language="JavaScript">/* This function was invoked by the activity */* The event is called by activities here's a picture we can download in its connection Android_waving.png */ Function Wave () {/* Here is a JavaScript custom function, which is called by our Android program, not in HTML */ document.getElementById ("Droid"). src= "Android_waving.png"; }</script> <body> <!--Calls into the JavaScript interface for the activity--- <!--calling functions in activity from an HTML file-- <!--that is, from HTML to Android program Note window.demo.clickOnAndroid () This sentence-- <a onclick="Window.demo.clickOnAndroid ()"><div style="width:80px;margin:0px Auto;padding:10px;Text-align:center;border:2px solid #202020; " > <!--pictures by default Android_normal.png -- id="Droid" src="Android_normal.png"/><br>Click me!</div></a> </body>
Here I made a comment, maybe you will not understand, no matter, the following we will look at the activity of the Code bar. Click to open link
Because of his long source code, I do not have a lot of all posted in, I just paste the important part of it .... Seems to be important, or full bar
Package com.Google.Android.Webviewdemo;Import android.app.Activity;Import Android.os.Bundle;Import Android.os.Handler;Import Android.util.Log;Import Android.webkit.JsResult;Import android.webkit.WebChromeClient;Import android.webkit.WebSettings;Import Android.webkit.WebView Public class Webviewdemo extends Activity { Private Static Final String Log_tag = "Webviewdemo"; Private WebView Mwebview; Private Handler Mhandler = New Handler(); @Override Public void onCreate(Bundle Icicle) { Super.onCreate(Icicle); Setcontentview(R.Layout.Main); Mwebview = (WebView) Findviewbyid(R.ID.WebView); websettings websettings = Mwebview.getsettings(); websettings.Setsavepassword(false); websettings.Setsaveformdata(false); // The following sentence is required and must be openedJavaScriptOtherwise, everything will be in vain. websettings.setjavascriptenabled(true); websettings.Setsupportzoom(false); Mwebview.setwebchromeclient(New mywebchromeclient()); // look, it's used here. Addjavascriptinterface that's the point of our focus. // we'll see him again.Demojavascriptinterfacethis class. And this class must be in the main thread. Mwebview.Addjavascriptinterface(New Demojavascriptinterface(), "Demo"); Mwebview.Loadurl("File:///android_asset/demo.html"); } // This is what he defines by Addjavascriptinterface provided by aObject Final class Demojavascriptinterface { Demojavascriptinterface() { } /** * This is not called on the UI Thread. Post a runnable to Invoke * This is not the calling interface thread. Publish a run call * Loadurl on the UI Thread. * Loadurlin theUIthread. */ Public void clickonandroid() { // Notice the name here. It isclickonandroid(),Attention, attention , serious attention. Mhandler.Post(New Runnable() { Public void Run() { // called here HTML in theJavaScript function Mwebview.Loadurl("Javascript:wave ()"); } }); } }// The code under the line can not look, debugging with the/////////////////////////////////////////////////////////////////////////////////////////////////// /** * provides a Hook for calling "Alert" from JavaScript. Useful for * fromJavaScripta "hint box" is provided in the . This is very useful. * Debugging your JavaScript. * Debug YourJavaScript. */ Final class mywebchromeclient extends webchromeclient { @Override Public Boolean Onjsalert(WebView View, String URL, String message, Jsresult result) { Log.D(Log_tag, message); result.Confirm(); return true; } }}
Here I also made a comment, where he defines a function in his class clickOnAndroid() , and I don't remember what we HTML中 saw in window.demo.clickOnAndroid() this statement. If you forget about it 翻上去再看一下 , especially when I say the attention of the place. Yes, here clickOnAndroid() is a function in our Android program. Maybe you'll ask that. Why is it windows.demo . Here demo we can see in
Mwebview . Addjavascriptinterface (newdemojavascriptinterface(),"Demo" );
It adds a character to it later in this sentence demo . I can understand that addJavascriptInterface the previous parameter is a method in HTML, and of course I prefer to say that he is a function. The demo is an object ... Oh, I am not familiar with this. As for this window I do not know why I only know that this program can be run correctly, if you know what it is then please tell me in the message board below, OK?
Now know how to call the Android program in HTML a way to do it. This is actually so simple, but the demo character I did not try he is not case-sensitive. If you're interested, then you can try it. It has no case sensitivity. Know how to call the Android method from the HTML, let me look at how it is from Android to HTML. In fact, this is very simple, just use
Mwebview . Loadurl ("Javascript:wave ()");
That's all you have to say. So my lord has read me a demo that Google gave me. Here is let me try it, try his this demo, because they gave the source code, so I do not post, everyone put his source paste up can achieve this
In the next episode, I'm going to show you how to use it addJavascriptInterface . Get a return value from a JavaScript function. The content of the article is not many, please forgive me
Android software development Webview.addjavascriptinterface cycle progressive "one"