Android WebView and js+html interact with Addjavascriptinterface and Webview.loadurl ("javascript:**");

Source: Internet
Author: User

To do a project recently, we need to display a service-supported interface with WebView. Ah, HTML, the white Snow JS is the same as nothing, I believe a lot of beginners dick silk staff like me,

HTML development tools do not know how to write. Hahaha ..... Now let's share the results.

,,

This is deliberately separated from the project demo here Some of the key code

SOURCE http://download.csdn.net/download/yung7086/7554309

Step first create an HTML file in the assets directory

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" > 


Play this all play for a long time should be for the direct Eclipse development write a function write n times teacher less comma curly braces not error .... HTML+JS development tools for the depressed seeking recommendation

Forget it or stick it in the whole piece. HTML calls to call these functions need to call the Java class here I need to define a Java class to implement the method that HTML calls

Package Com.example.jsdemo;import Android.content.componentname;import Android.content.context;import Android.content.intent;import Android.content.pm.applicationinfo;import Android.content.pm.packagemanager;import Android.content.pm.packagemanager.namenotfoundexception;import Android.net.uri;import         android.webkit.javascriptinterface;/** * JS Method of Call * * @author yung * <p> * June 24, 2014 09:26:14 * <p> * This class is opened in QQ and is directly called through the package name and class name although the QQ package name is not easy to change but the main interface good things may change * If found not open QQ app can see whether it is QQ upgrade changed the class name */public CLA SS Androidjavascript {Context C; string[] Qqpackage = new string[] {"Com.tencent.mobileqq", "com.tencent.mobileqq.activity.SplashActivity"}; string[] Wxpackage = new string[] {"com.tencent.mm", "Com.tencent.mm.ui.LauncherUI"};p ublic Androidjavascript (Context c) {this.c = C;} @JavascriptInterfacepublic void Callphone (final String telphone) {Intent Intent = new Intent (Intent.action_call, Uri.parse ("Tel:" + telphone)); c.startactivity (intent);} @JavascriptInterfacepublic void Callqq (String qq) {//Implement call phone number if (!checkbrowser (Qqpackage[0])) {} else {Intent Intent = new Intent (); ComponentName cmp = new ComponentName (qqpackage[0], qqpackage[1]); Intent.setaction (Intent.action_main); Intent.addcategory (Intent.category_launcher); Intent.addflags (Intent.flag_activity_new_task); Intent.setcomponent (CMP); c.startactivity (intent);}} @JavascriptInterfacepublic void Callweixin (String weixin) {if (!checkbrowser (Wxpackage[0])) {} else {Intent Intent = new I Ntent (); ComponentName cmp = new ComponentName (wxpackage[0], wxpackage[1]); Intent.setaction (Intent.action_main); Intent.addcategory (Intent.category_launcher); Intent.addflags (Intent.flag_activity_new_task); Intent.setcomponent (CMP); c.startactivity (intent);}} Gets the source code @javascriptinterfacepublic void GetSource (String htmlstr) {//LOG.E ("HTML", HTMLSTR) of the HTML generated by JS on WebView;// String path = C.getfilesdir (). GetAbsolutePath () + "/serve.html"; Data/data Directory}//The application that detects the package name is already installed on the phone public boolean checkbrowser (String PackageName) {if (PackageName = = NULL | | ". Equals (PackageName)) return false;try {applicationinfo info = C.getpackagemanager (). Getapplicationinfo ( PackageName, packagemanager.get_uninstalled_packages); return true;} catch (Namenotfoundexception e) {return false;}}}
Now there is a way to have HTML on how the difference is called so remember to set WebView must be parameters

Mywebview.addjavascriptinterface (New Androidjavascript (This), "Android");

Do you find that the HTML JS method has Jiangzi statements

<a href= ' javascript:Android.callWeixin (\ "" +child[n]+ "\") ' > '

Indeed this inside "Android" and "Mywebview.addjavascriptinterface" (New Androidjavascript (this), "Android"), must be the same OH.

Here, note the double quotes, huh?

WebSettings websettings = Mywebview.getsettings (); websettings.setjavascriptenabled (true); IsExistsHTML ();// Mywebview.loadurl ("file:///android_asset/ss.html"); String path = Getfilesdir (). GetAbsolutePath () + HTMLName; Data/data directory Mywebview.loadurl ("file:///" + path); Mywebview.addjavascriptinterface (new Androidjavascript (this), " Android ");//Mywebview.loadurl (" Javascript:getstr (' "+ 122222 +" ') "); Mywebview.setwebviewclient (WEBVIEWCILNT);

Okay, here we go. The method that calls the Java class implementation in HTML,

The following say I QQ number and phone is from the server to get how to add to the HTML up. This is JS's mission.

You see my HTML is no layout, the data are JS generated interface,.

How to upload from Java to JS inside? Write Mywebview.loadurl in Java ("Javascript:createtable ('" + Phon + ")");

CreateTable ("") is the function name of JS. Remember this mywebview.loadurl ("javascript:createtable ('" + Phon + "')")

In OnCreate should not be able to need Jiangzi

Mywebview.setwebviewclient (WEBVIEWCILNT); Webviewclient webviewcilnt = new Webviewclient () {@Overridepublic void Onreceivederror (WebView view, int errorCode, String description, String failingurl) {} @Overridepublic void onpagefinished (WebView view, string url) { super.onpagefinished (view, URL); String Phon = Loadhtmldata (); Mywebview.loadurl ("Javascript:createtable ('" + Phon + "')");// Gets the WebView loaded HTML page view.loadurl ("Javascript:window. Android.getsource (' 

Wait WebView initialization HTML after completion in the call JS oh so reprint please indicate the source

Respecting the author yung7086



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.