Java and JavaScript interaction on WebView

Source: Internet
Author: User

In Android development, with the development efficiency and interface updates in mind, sometimes using webview combined with Web page technology, mobile applications can be quickly and iteratively developed. WebView load resources is not slow, but if there are more resources, it is slow. Pictures, CSS, JS, html each of these resources will probably need to 10-200ms, generally 30ms or less on the line. However, WebView is waiting for all the resources to be loaded to finish the rendering, so finally use native JS to write, do not use too much jquery and other frameworks, to improve the user experience.

In hybrid development, the Android native SDK is sometimes used, such as calling a camera, viewing an album, recording, and so on, which requires JavaScript in the Web page to be called to the Android SDK interface. Because Android's WebView is based on the WebKit kernel, it integrates the interface functions of JS and Java intermodulation, which can be easily used for development.

Code:

1 <Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Xmlns:tools= "Http://schemas.android.com/tools"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent" >5 6     <WebView7         Android:id= "@+id/webview"8 Android:layout_width= "Fill_parent"9 Android:layout_height= "Fill_parent"Ten Android:layout_above= "@+id/linearlayout" One          /> A     <LinearLayout -         Android:id= "@+id/linearlayout" - Android:layout_width= "Match_parent" the Android:layout_height= "Wrap_content" - Android:layout_alignparentbottom= "true" -         > -         <Button +             Android:id= "@+id/btn" - Android:layout_width= "Wrap_content" + Android:layout_height= "Wrap_content" A Android:text= "Java invoke JavaScript interface" at             > -         </Button> -     </LinearLayout> - </Relativelayout>

Java and JavaScript interaction on WebView

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.