Interaction between webView and javascript in Android

Source: Internet
Author: User

AddJavaScipteInterface (Object obj, String Interfacename) is used to bind a java Object to a javaste Object. The javaScripte Object Name Is interfaceName.

Package com.king.android.net;

Import android. app. Activity;
Import android. OS. Bundle;
Import android. webkit. WebView;

Import com. king. android. R;

/**

* Description: mutual use of webview and js
* Author: Andy. Liu
* Time: 08:24:06
**/
Public class WebAndJsActivity extends Activity {
Private WebView mWebView = null;
Private PersonalData mPersonalData; // save data

@ Override
Protected void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

SetContentView (R. layout. webview_layout );
MPersonalData = new PersonalData ();
MWebView = (WebView) findViewById (R. id. wv_view );
MWebView. getSettings (). setJavaScriptEnabled (true );
// Add an instance of this class to the global javascript Object window, so that you can use windowPersonData to call its method.
MWebView. addJavascriptInterface (this, "PersonalData ");
// Load the webpage
MWebView. loadUrl ("file: // android_asset/PersonalData.html ");
}

Public PersonalData getPersonalData (){
Return mPersonalData;
}

// Call the information displayed in the js script
Class PersonalData
{
String mID;
String mName;
String mAge;
String mBlog;
Public PersonalData ()
{
This. mID = "123456789 ";
This. mName = "Android ";
This. mAge = "100 ";
This. mBlog = "http://xxx.javaeye.com ";
}
Public String getID ()
{
Return mID;
}
Public String getName ()
{
Return mName;
}
Public String getAge ()
{
Return mAge;
}
Public String getBlog ()
{
Return mBlog;
}
}

}

Js Script: window. onload = function (){

Var personaldata = window. PersonalData. getPersonalData ();
If (personaldata)
{
Var Personaldata = document. getElementById ("Personaldata ");
Pnode = document. createElement ("p ");
Tnode = document. createTextNode ("ID:" + personaldata. getID ());
Pnode. appendChild (tnode );
Personaldata. appendChild (pnode );
Pnode = document. createElement ("p ");
Tnode = document. createTextNode ("Name:" + personaldata. getName ());
Pnode. appendChild (tnode );
Personaldata. appendChild (pnode );
Pnode = document. createElement ("p ");
Tnode = document. createTextNode ("Age:" + personaldata. getAge ());
Pnode. appendChild (tnode );
Personaldata. appendChild (pnode );
Pnode = document. createElement ("p ");
Tnode = document. createTextNode ("Blog:" + personaldata. getBlog ());
Pnode. appendChild (tnode );
Personaldata. appendChild (pnode );
}

}

 

Of course, you can also directly call js methods in java code, so that you can call each other to obtain data.

WebView. loadUrl ("javascpite: Method Name ()");

 

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.