如何讓javascript調用android中的函數?

來源:互聯網
上載者:User

如何讓javascript調用android中的函數?
本篇通過一個執行個體來說明如何在javascript中去調用android的函數,其實方法也比較簡單,只要調用addJavascriptInterface方法即可映射一個java對象到javascript對象。

Android代碼

  1. package com.macernow.djstava;

  2. import android.support.v7.app.ActionBarActivity;
  3. import android.os.Bundle;
  4. import android.util.Log;
  5. import android.view.Menu;
  6. import android.view.MenuItem;
  7. import android.webkit.JavascriptInterface;
  8. import android.webkit.WebView;


  9. public class MainActivity extends ActionBarActivity {

  10. private static final String TAG = "MainActivity";
  11. private WebView webView;

  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_main);

  16. webView = (WebView)findViewById(R.id.webView);
  17. webView.getSettings().setJavaScriptEnabled(true);
  18. webView.addJavascriptInterface(this,"djstava");
  19. webView.loadUrl("http://192.168.31.109/demo.html");

  20. }

  21. @JavascriptInterface
  22. public void jsextent() {
  23. Log.e(TAG,"========== js extension success.");
  24. }

  25. @Override
  26. public boolean onCreateOptionsMenu(Menu menu) {
  27. // Inflate the menu; this adds items to the action bar if it is present.
  28. getMenuInflater().inflate(R.menu.menu_main, menu);
  29. return true;
  30. }

  31. @Override
  32. public boolean onOptionsItemSelected(MenuItem item) {
  33. // Handle action bar item clicks here. The action bar will
  34. // automatically handle clicks on the Home/Up button, so long
  35. // as you specify a parent activity in AndroidManifest.xml.
  36. int id = item.getItemId();

  37. //noinspection SimplifiableIfStatement
  38. if (id == R.id.action_settings) {
  39. return true;
  40. }

  41. return super.onOptionsItemSelected(item);
  42. }
  43. }
WebView一定要使能javascript。在Android 4.2之後,拋出給javascript的java方法必須加上@JavascriptInterface。

布局檔案
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
  3. android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
  4. android:paddingRight="@dimen/activity_horizontal_margin"
  5. android:paddingTop="@dimen/activity_vertical_margin"
  6. android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

  7. <WebView
  8. android:id="@+id/webView"
  9. android:layout_width="fill_parent"
  10. android:layout_height="fill_parent">
  11. </WebView>
  12. </RelativeLayout>


測試調用

在javascript指令碼中調用java方法


  1. window.djstava.jsextent()

掃描關注公眾號


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.