WebView Hybrid Development

Source: Internet
Author: User

Now the way to develop apps, not traditional app development, there are a lot of apps slowly to the development of mixed mode, using WebView is the traditional development mode to the hybrid mode of bridge tools, combined with a lot of web front-end development interface, so that the development of speed, The following describes the basic use of WebView in Android.


in the development process should pay attention to several points:

The license "Android.permission.INTERNET" must be used in 1.androidmanifest.xml, otherwise the Web page not available error will occur.
2. If there is JavaScript on the page visited, the WebView must be set to support JavaScript.
Webview.getsettings (). Setjavascriptenabled (True);

3. If the page links, if you want to click on the link to continue in the current browser response, instead of the new Android system browser should link in the ring, you must overwrite the WebView Webviewclient object.

Such as:

    1.  mwebview.setwebviewclient (New Webviewclient () {      
                  Public boolean shouldoverrideurlloading (WebView view, String URL) {      
          &NB Sp           view.loadurl (URL);//continue to respond in the current WebView browser       
        & nbsp             return true;      &NBSP,
                 
                 &NBS P  });

4. If you do not do any processing, browse the Web page, click the System "back" button, the entire browser will call finish () and end itself, if you want to browse the page fallback rather than launch the browser, you need to process and consume the back event in the current activity.

    1.  public boolean onKeyDown (int keycode, keyevent event) {      
              I F ((keycode = = keyevent.keycode_back) && mwebview.cangoback ()) {      
          &nbs P       Mwebview.goback ()//processing WebView return       
                &N Bsp return true;      &NBSP,
             }       ,
              Retu RN Super.onkeydown (KeyCode, event);       
         } 

Next, let's look at how WebView in Android supports Javascripte custom objects, and JS has window,history,document standard objects in the standard. Similarly we can in the development of the browser when we define our own object call phone system function to handle, so use JS can do whatever you like.

See an example:

public class Webviewdemo extends Activity {
Private WebView Mwebview;
Private Handler Mhandler = new Handler ();
public void OnCreate (Bundle icicle) {
Super.oncreate (Icicle);
Setcontentview (R.layout.webviewdemo);
Mwebview = (WebView) Findviewbyid (R.id.webview);
WebSettings websettings = Mwebview.getsettings ();
Websettings.setjavascriptenabled (TRUE); Setting JS in the WebView support interface
Mwebview.addjavascriptinterface (New Object () {
public void Clickonandroid () {
Mhandler.post (New Runnable () {
public void Run () {
                        Mwebvie W.loadurl ("Javascript:wave ()");//Invoke interface JS function       
               });       
           }     &N bsp; 
        Mwebview.loadurl ("file:/// Android_asset/demo.html "); Set load Local page interface      

Let's look at Addjavascriptinterface (object obj,string InterfaceName), a method that binds a Java object to a JavaScript object, and the JavaScript object name is InterfaceName (Demo), scope is global. Once the webview is initialized, the bound Java object can be accessed directly through Javascript:window.demo in the WebView loaded page. Let's see how it's called in HTML.

<mce:script language= "JavaScript" ><!--


Function Wave () {
document.getElementById ("Droid"). src= "Android_waving.png";
}

--></mce:script>
<body>
<a onclick= "window.demo.clickOnAndroid ()" >
<br>
Click me!
</a>
</body>

This makes it possible to invoke the Java object's Clickonandroid () method in JavaScript, and we can define a number of methods in this object (such as texting, calling the contact list, and so on-phone system functions. Here, the wave () method is an example of invoking JavaScript in Java.

Here are a few more things to know:

1) in order for WebView to load the Assets,android SDK from the APK file, a schema is provided with the prefix "file:///android_asset/". WebView encountered such a schema, go to the current package in the assets directory to find content. As in the above "file:///android_asset/demo.html"
2) The Java objects and methods to be bound in the Addjavascriptinterface method are run in another thread and cannot be run in the thread that constructs them, which is also the purpose of using handler.

WebView Hybrid Development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.