WebView Basic Application Example--android using WebView to open a Web page within an app

Source: Internet
Author: User

Recently used WebView to load the URL Display Web page in the app, by pasting a simple example.

For general applications, there is no need for complex processing logic when it comes to displaying Web pages, but the novice may encounter a problem with the process: (Daniel can ignore it)

1, call the Webview.loadurl () method, will still invoke the system browser;

2. Press the Back button to exit the interface directly instead of returning to the previous level of the open Web page.

In fact, if only a simple display of the Web page, just a bit

1, to WebView set their own definition of webviewclient;

2. Cover the Shouldoverrideurlloading method in Webviewclient ().

At this point, the first problem has been resolved, no longer invoke the phone's own browser, and implemented within the application to open the link display.

3, covering the activity OnKeydown method, from which to determine whether the Web page can return to the previous page, if there is no return to the previous page, the activity return key logic.

After completing these three parts, the simple use of webview can be achieved. If you need more complex processing, such as executing JS code to call the phone or other more advanced functions, please do further research.


On the code:

public class Publicwebviewactivity extends Baseactivity{private WebView WebView; @Overrideprotected void OnCreate ( Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_public_webview); WebView = (WebView) Findviewbyid (R.id.webview);//Use the custom Webviewclientwebview.setwebviewclient (new Webviewclient () {/ /Overwrite Shouldoverrideurlloading method @overridepublic Boolean shouldoverrideurlloading (WebView view, String URL) { View.loadurl (URL); return true;}); Webview.loadurl (URL);} Overwrite onKeyDown add processing WebView interface returns event Handling @overridepublic boolean onKeyDown (int keycode, keyevent event) {if (keycode = = Keyeven T.keycode_back && Webview.cangoback ()) {webview.goback ();//Return to previous page return true;} Return Super.onkeydown (KeyCode, event);}}


WebView Basic Application Example--android using WebView to open a Web page within an app

Related Article

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.