Android interacts with H5 (Java vs. js)

Source: Internet
Author: User

1.Android Java CodeJava code

Package Com.wytiger.jsdemo;

Import android.app.Activity;
Import Android.os.Bundle;
Import Android.content.Context;
Import Android.graphics.Color;
Import Android.view.View;
Import Android.webkit.JavascriptInterface;
Import android.webkit.WebChromeClient;
Import Android.webkit.WebView;
Import Android.webkit.JsResult;
Import Android.widget.Button;
Import Android.widget.Toast;

public class JsActivity1 extends Activity {

Private Button mBtn;
Private WebView Mwebview;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.LAYOUT.ACTIVITY_JS1);

Initialization
Initviews ();

Initwebview ();
}

private void Initwebview () {
Set encoding
Mwebview.getsettings (). Setdefaulttextencodingname ("Utf-8");
Set Background color transparency
Mwebview.setbackgroundcolor (Color.argb (0, 0, 0, 0));
If you do not set this, the button in the JS code will be displayed, but press down without popping the dialog box
Mwebview.setwebchromeclient (New Webchromeclient () {

@Override
public boolean Onjsalert (WebView view, string URL, String message,
Jsresult result) {
Return Super.onjsalert (view, URL, message, result);
}

});

Support Java Call JS
Mwebview.getsettings (). Setjavascriptenabled (True);
Loading HTML pages
Mwebview.loadurl ("file:///android_asset/js_test.html");
Set local Call object and object name
JS uses this object name to invoke the Java local method
Androidobj javaobj = new Androidobj (this);
Mwebview.addjavascriptinterface (Javaobj, "javaobj");
}

private void Initviews () {
MBtn = (Button) Findviewbyid (R.ID.BTN);
Mwebview = (WebView) Findviewbyid (R.id.webview);

Java calls the JS method
Mbtn.setonclicklistener (New View.onclicklistener () {

@Override
public void OnClick (View v) {
Invoking the JS method
Mwebview.loadurl ("Javascript:calljs ()");
Toast.maketext (Jsactivity1.this, "Call JS's Calljs () method",
Toast.length_long). Show ();
}
});

}

public class Androidobj {
Context Mcontxt;

Public Androidobj (Context mcontxt) {
This.mcontxt = Mcontxt;
}

API17 version above plus annotations
@JavascriptInterface
public void Calljava (String name) {
Toast.maketext (mcontxt, Name, Toast.length_long). Show ();
}

public void CallJava2 (String name) {
Toast.maketext (Mcontxt, "Call CallJava2 Method:" + Name, Toast.length_short)
. Show ();
}
}

}

Layout file

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
tools:context= "Com.wytiger.jsdemo.MainActivity" >

<button
Android:id= "@+id/btn"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:text= "Java Invoke JS method"/>


<view
Android:layout_width= "Match_parent"
android:layout_height= "1DP"
android:layout_below= "@+id/btn"
android:background= "#ff0000"/>

<webview
Android:id= "@+id/webview"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
Android:layout_alignparentleft= "true"
android:layout_below= "@+id/btn"
android:layout_margintop= "10DP"/>

</RelativeLayout>

2.h5 JS Code, put this HTML file into the Assets folder can be


<title> test Android Java with H5 JavaScript interaction </title>

<style>
Body {background-color: #e6e6e6}
. Rect
{
Color:white;
Font-family:verdana, Arial, Helvetica, Sans-serif;
font-size:16px;
width:100px;
padding:6px;
Background-color: #98bf21;
Text-decoration:none;
Text-align:center;
Border:none;
Cursor:pointer;
}
. Inputstyle {FONT-SIZE:16PX;PADDING:6PX}
</style>


<body>
<p> This is H5 page </p>
<a class = "rect" onclick= "Sendinfotojava ()" Call JS method in >java </a>
<script>
function Calljs () {
document.getElementById ("Helloweb"). Innerhtml= "Hello,i ' m JS";
}

var aTag = document.getElementsByTagName (' a ') [0];
Atag.addeventlistener (' click ', function () {
H5 JS Call Android Java native method
Javaobj.calljava ("JS call Java Method Calljava (String name)!! ");
return false;
}, False);
</script>

<div id= "Helloweb" ></div>
</body>

Finally, recommend an open Source library: Jsbridge

Android interacts with H5 (Java vs. js)

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.