Android API and JavaScript call each other

Source: Internet
Author: User

Not to mention, first run the Code. This is the page code we load:

1. How does JavaScript call Android functions:

Webview = new webview (this); webview. getsettings (). setjavascriptenabled (true); webview. addjavascriptinterface (New locationjsobj (this), "android"); webview. addjavascriptinterface (New packagejsobj (this), "package"); webview. loadurl ("file: // android_asset/location.html"); Class locationjsobj {private final context con; Public locationjsobj (context con) {This. con = con;} Public String GPS (string top, string E Nd) {return top + "Degrees:" + 20.5555 + ", degrees:" + 36.4522 + end;} class packagejsobj {private final context mcontext; public packagejsobj (context) {mcontext = context;} Public String ispackageinstall (string pacakgename) {Boolean isinstalled = false; packagemanager PM = mcontext. getpackagemanager (); try {packageinfo = PM. getpackageinfo (pacakgename, 0); If (null! = Packageinfo) {isinstalled = true ;}} catch (namenotfoundexception e) {e. printstacktrace () ;}return pacakgename + "is install:" + isinstalled ;}}

2. the android code calls the functional functions of javascript:

webview = new WebView(this);webview.getSettings().setJavaScriptEnabled(true);webview.loadUrl("file:///android_asset/Location.html");Button button = new Button(this);button.setText("Add new button on web");button.setOnClickListener(mButtOnClickListener);private final OnClickListener mButtOnClickListener = new OnClickListener() {@Overridepublic void onClick(View v) {webview.loadUrl("javascript:addButton()");}};

Complete code after merging:

Package COM. example. javascripttest; import android. app. activity; import android. content. context; import android. content. PM. packageinfo; import android. content. PM. packagemanager; import android. content. PM. packagemanager. namenotfoundexception; import android. OS. bundle; import android. view. view; import android. view. view. onclicklistener; import android. webKit. webview; import android. widget. button; import android. Widget. linearlayout; public class javascripttest extends activity {private webview;/** called when the activity is first created. * // @ overridepublic void oncreate (bundle icicicle) {super. oncreate (icicle); setcontentview (generateconentview ();} private linearlayout generateconentview () {linearlayout = new linearlayout (this); linearlayout. setlayoutparams (New linearlayout. layoutparams (Li Nearlayout. layoutparams. fill_parent, linearlayout. layoutparams. fill_parent); linearlayout. setorientation (linearlayout. vertical); webview = new webview (this); webview. getsettings (). setjavascriptenabled (true); webview. addjavascriptinterface (New locationjsobj (this), "android"); webview. addjavascriptinterface (New packagejsobj (this), "package"); webview. loadurl ("file: // android_asset/location.html"); linearla Yout. layoutparams wvlayoutparams = new linearlayout. layoutparams (linearlayout. layoutparams. fill_parent, linearlayout. layoutparams. wrap_content); wvlayoutparams. weight = 1.0f; webview. setlayoutparams (wvlayoutparams); linearlayout. addview (webview); button = new button (this); button. setlayoutparams (New linearlayout. layoutparams (linearlayout. layoutparams. fill_parent, linearlayout. layoutparams. wrap_con Tent); button. settext ("Add new button on Web"); button. setonclicklistener (mbuttonclicklistener); linearlayout. addview (button); Return linearlayout;} private final onclicklistener mbuttonclicklistener = new onclicklistener () {@ overridepublic void onclick (view v) {webview. loadurl ("javascript: addbutton ()") ;}}; class locationjsobj {private final context con; Public locationjsobj (context con) {This. con = con ;} Public String GPS (string top, string end) {return top + "Degrees:" + 20.5555 + ", degrees:" + 36.4522 + end ;}} class packagejsobj {private final context mcontext; Public packagejsobj (context) {mcontext = context;} Public String ispackageinstall (string pacakgename) {Boolean isinstalled = false; packagemanager PM = mcontext. getpackagemanager (); try {packageinfo = PM. getpackageinfo (pacakgename, 0); If (null! = Packageinfo) {isinstalled = true ;}} catch (namenotfoundexception e) {e. printstacktrace () ;}return pacakgename + "is install:" + isinstalled ;}}}

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.