Android user interface-widget-network view webview

Source: Internet
Author: User

A simple example of webview.

Pay attention to the following points during development:
1. androidmanifest. xml must use the permission "android. Permission. Internet". Otherwise, the web page not available error may occur.
2. If the accessed page contains JavaScript, webview must be set to support JavaScript.
Webview. getsettings (). setjavascriptenabled (true );
3. If you want to click the link on the page to continue responding in the current browser, instead of making a response in the browser of the new Android system, you must overwrite the webviewclient object of webview.

Mwebview. setwebviewclient (New webviewclient (){
Public Boolean shouldoverrideurlloading (webview view, string URL ){
View. loadurl (URL );
Return true;
}
});
Mwebview. setwebviewclient (New webviewclient (){
Public Boolean shouldoverrideurlloading (webview view, string URL ){
View. loadurl (URL );
Return true;
}
});

4. if you do not do any processing, browse the Web page and click the system "back" key, the entire browser will call finish () to end itself. If you want to browse the Web page back rather than launch the browser, the back event needs to be processed and consumed in the current activity.

Public Boolean onkeydown (INT keycode, keyevent event ){
If (keycode = keyevent. keycode_back) & mwebview. cangoback ()){
Mwebview. Goback ();
Return true;
}
Return super. onkeydown (keycode, event );
}
Public Boolean onkeydown (INT keycode, keyevent event ){
If (keycode = keyevent. keycode_back) & mwebview. cangoback ()){
Mwebview. Goback ();
Return true;
}
Return super. onkeydown (keycode, event );
}

Next, let's take a look at how webview in Android supports javascripte custom objects. In W3C standard, JS contains standard objects such as window, history, and document, similarly, when developing a browser, we can define our own object to call the function of the mobile phone system for processing, so that we can use js to do whatever we want.

View an instance:

View plaincopy to clipboardprint?
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 = mwebview. getsettings ();
Websettings. setjavascriptenabled (true );
Mwebview. addjavascriptinterface (new object (){
Public void clickonandroid (){
Mhandler. Post (New runnable (){
Public void run (){
Mwebview. loadurl ("javascript: Wave ()");
}
});
}
}, "Demo ");
Mwebview. loadurl ("file: // android_asset/demo.html ");
}
}
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 = mwebview. getsettings ();
Websettings. setjavascriptenabled (true );
Mwebview. addjavascriptinterface (new object (){
Public void clickonandroid (){
Mhandler. Post (New runnable (){
Public void run (){
Mwebview. loadurl ("javascript: Wave ()");
}
});
}
}, "Demo ");
Mwebview. loadurl ("file: // android_asset/demo.html ");
}
}

The addjavascriptinterface (Object OBJ, string interfacename) method binds a Java object to a JavaScript Object. The JavaScript Object Name Is interfacename (DEMO) and the scope is global. After webview initialization, you can directly access the bound Java object through javascript: window. demo on the page loaded by webview. Let's see how html is called.

<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>
</Html>
<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>
</Html>

In this way, you can call the clickonandroid () method of the Java object in Javascript. We can also define many methods in this object (such as sending text messages and calling the contact list and other mobile phone system functions .), Here, the wave () method is an example of calling Javascript in Java.

Here are some knowledge points:

1) To allow webview to load assets from the APK file, the android SDK provides a schema prefixed with "file: // android_asset /". When webview encounters such a schema, it finds the content in the Assets Directory of the current package. Above "file: // android_asset/demo.html"
2) the Java object and method to be bound in the addjavascriptinterface method must run in another thread and cannot run in the construction of another thread. This is also the purpose of using handler.

 

 

 

Summary:

1. add permissions: The permission "android. Permission. Internet" must be used in androidmanifest. xml. Otherwise, the web page not available error may occur.
2. Generate a webview component in the activity: webview = new webview (this );
3. Set Basic webview information:
If the accessed page contains JavaScript, webview must be set to support JavaScript.
Webview. getsettings (). setjavascriptenabled (true );
Touch focus Function
Requestfocus ();
Cancel scroll bar
This. setscrollbarstyle (scrollbars_outside_overlay );
4. Set the web page to be displayed in wevview:
For Internet: webview. loadurl ("http://www.google.com ");
Local file: webview. loadurl ("file: // android_asset/xx.html"); local files are stored in the assets file.
5. If you want to click the link, you can handle it by yourself, instead of clicking the link in the browser of the new Android system.
Add an event listening object (webviewclient) to webview)

And rewrite some of the methods.
Shouldoverrideurlloading: Response to the hyperlink button on the webpage.
When you press a connection, webviewclient will call this method and pass the parameter: URL
Onloadresource
Onpagestart
Onpagefinish
Onreceiveerror
Onreceivedhttpauthrequest

6. If you use the webview link to view many pages without any processing, click the system "back" key and the entire browser will call finish () to end itself, if you want to roll back the web page instead of exiting the browser, you need to process and consume the back event in the current activity.
Override the onkeydown (INT keycoder, keyevent event) method of the activity class.

  1. Public Boolean onkeydown (INT keycoder, keyevent event ){
  2. If (webview. cangoback () & keycoder = keyevent. keycode_back ){
  3. Webview. Goback (); // Goback () indicates that the previous page of webview is returned.
  4. Return true;
  5. }
  6. Return false;
  7. }

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.