Android API與Javascript之間互相調用

來源:互聯網
上載者:User

不多說,先上代碼,這是我們載入的頁面代碼:

<html><head><title>JS calls Android Method</title><script language="javascript">function addButton() {var html = "<input type=\"button\" value=\"New Button\" onclick=\"newButton();\">";document.getElementById("add").innerHTML = html;}function newButton() {alert("I am new button");}</script></head><body><h1>JS on Android</h1><script type="text/javascript">document.write(android.gps("<i>", "</i>"));</script></br><h3><script type="text/javascript">document.write(package.isPackageInstall("com.dolphin.browser.cn"));</script></h3></br><p>New button will show below:</p><div id="add"></div></body></html>

1. 接下來是Javascript如何調用Android系統功能:

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 end) {return top + "緯度:" + 20.5555 + ", 経度: " + 36.4522 + end;}}class PackageJsObj{private final Context mContext;public PackageJsObj(Context context){mContext = context;}public String isPackageInstall(String pacakgeName){boolean isInstalled = false;PackageManager pm = mContext.getPackageManager();try {PackageInfo packageInfo = pm.getPackageInfo(pacakgeName, 0);if(null != packageInfo){isInstalled = true;}} catch (NameNotFoundException e) {e.printStackTrace();}return  pacakgeName + " is install:" + isInstalled;}}

2. Android代碼調用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()");}};

合并後,完整代碼:

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 webview;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle icicle) {super.onCreate(icicle);setContentView(generateConentView());}private LinearLayout generateConentView(){LinearLayout linearLayout = new LinearLayout(this);linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.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");LinearLayout.LayoutParams wvLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);wvLayoutParams.weight = 1.0F;webview.setLayoutParams(wvLayoutParams);linearLayout.addView(webview);Button button = new Button(this);button.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));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 + "緯度:" + 20.5555 + ", 経度: " + 36.4522 + end;}}class PackageJsObj{private final Context mContext;public PackageJsObj(Context context){mContext = context;}public String isPackageInstall(String pacakgeName){boolean isInstalled = false;PackageManager pm = mContext.getPackageManager();try {PackageInfo packageInfo = pm.getPackageInfo(pacakgeName, 0);if(null != packageInfo){isInstalled = true;}} catch (NameNotFoundException e) {e.printStackTrace();}return  pacakgeName + " is install:" + isInstalled;}}}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.