Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7965929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:17:04+00:00 2026-06-04T06:17:04+00:00

I have added a Javascript interface object to a WebView using the addJavascriptInterface() method

  • 0

I have added a Javascript interface object to a WebView using the addJavascriptInterface() method and I am trying to invoke a method on that object using WebView.loadUrl().

The object is a simple class called JSInterface. It has a single method called echo() that takes no parameters and returns void. The URL I am passing to loadUrl() is "javascript:JSInterface.echo()" (JSInterface is also the interface name I used for addJavascriptInterface()).

When I run the attached app, the echo() method never executes. There are no errors in the logcat or anywhere else. What do I need to do to invoke echo() from Javascript?

This is the activity with all source.


It is of course right after I post the question that I figure out what’s wrong. In the attached code I am calling addJavascriptInterface() from a thread other than the UI thread. Moving addJavascriptInterface() to onCreate() fixes the problem.


package test.pg;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;

public class TestActivity2
  extends Activity
{
  WebView webview;

  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    webview = (WebView) findViewById(R.id.webview);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.loadUrl("file:///android_asset/www/invoke.html");
    TimerThread t = new TimerThread(webview);
    t.start();
  }

  class TimerThread
    extends Thread
  {
    long    lastRun;
    WebView appView;

    public TimerThread(WebView appView)
    {
      lastRun = System.currentTimeMillis();
      this.appView = appView;
    }

    public void run()
    {
      try
      {
        while (true)
        {
          if (System.currentTimeMillis() - lastRun < 5000)
          {
            Thread.sleep(5000);
          }
          else
          {
            appView.addJavascriptInterface(new JSInterface(), "JSInterface");
            InvokerThread t = new InvokerThread(appView);
            t.start();
            lastRun = System.currentTimeMillis();
          }
        }
      }
      catch (Exception e)
      {
        Log.e("InvokerThread", "exception=" + e.toString(), e);
      }
    }
  }

  class InvokerThread
    extends Thread
  {
    WebView appView;

    public InvokerThread(WebView appView)
    {
      this.appView = appView;
    }

    public void run()
    {
      String function = "javascript:JSInterface.echo()";
      appView.loadUrl(function);
    }
  }

  class JSInterface
  {
    public void echo()
    {
      System.out.println("JSInterface.echo()");
      Log.i("JSInterface", "JSInterface.echo()");
    }
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T06:17:05+00:00Added an answer on June 4, 2026 at 6:17 am

    It is of course right after I post the question that I figure out what’s wrong. In the attached code I am calling addJavascriptInterface() from a thread other than the UI thread. Moving addJavascriptInterface() to onCreate() fixes the problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have added a JavaScript Interface to WebView. I am able to use all
I have controls that are dynamically added via javascript. I cannot set the class
I have added text animation using javascript function: <div id=stats> <div class=textEffects> Text 1
I have added following table dynamically using Javascript: <table class=ActionMenu> <tr> <td> <p>Ghanshyam</p> </td>
Okay, so i have the following html added to a site using javascript/greasemonkey. (just
I have several pages that using the prototype library, so, I added javascript_include_tag 'prototype'
I have a jsf component that must access to a javascript file, i added
I have added the JavaScript that I need to the bottom of my pages
I have added a WebView to a Cocoa application and I'm trying to figure
how to append error messages on validation summary through javascript. i have added a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.