How can javascript call functions in android?

Source: Internet
Author: User

How can javascript call functions in android?
This article uses an example to illustrate how to call android functions in javascript. In fact, the method is relatively simple. You only need to call the addJavascriptInterface method to map a java object to a javascript Object.

Android code

 
 
  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 must enable javascript. After Android 4.2, @ JavascriptInterface must be added to the java method thrown to javascript.

Layout File
 
 
  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>


Test Call

Call the java method in a javascript script


 
 
  1. window.djstava.jsextent()

Scan the public account


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.