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 7039661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:49:32+00:00 2026-05-28T01:49:32+00:00

I have a Native application, which has a WebView in it. I add a

  • 0

I have a Native application, which has a WebView in it.
I add a class to the WebView to be able to call it from JavaScript like so:

webView.addJavascriptInterface(new JavascriptInterface(webView, handler), "Android.JSI");

I then call the JavaScriptInterface class from JavaScript. My HTML/JavaScript looks like:

<input type="button" value="Sleep Alert" onclick="sleepAlert(sleepAlertCallbackSuccess, sleepAlertCallbackFail, 'Hi Sleep Alert');" />

function sleepAlert(callBackSuccess, callBackFailure, message)
{                       
    window.Android.JSI.sleepAlert(callBackSuccess.name, callBackFailure.name, message);
}

function sleepAlertCallbackSuccess(message)
{
    alert("success: " + message);
}

function sleepAlertCallbackFail(message)
{
    alert("fail: " + message);
}

I then callback into the JavaScript like so:

js = "javascript:" + callbackSuccess + "('" + message + "')";
webView.loadUrl(js);

This works, but I am kind of stuck on if the user wants to pass in an anonymous function instead of a normal named function. I.E.

<input type="button" value="Anonymous call back" onclick="sleepAlert(function(message) { alert(message); }, sleepAlertCallbackFail, 'anonymous sleep alert');" />

When I pass this in to my Java class

public void sleepAlert(final String callbackSuccess, String callbackFail, final String message)

It says callbackSuccess is undefined.

Any ideas on the proper way to do this as I feel im getting lost going the wrong way?

  • 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-05-28T01:49:32+00:00Added an answer on May 28, 2026 at 1:49 am

    Set it to a variable. You can do something like:

    var a = <whatever>; a(message);
    

    Then if it’s a method it’ll resolve to:

    var a = sleepAlertCallbackSuccess; a(message);
    

    or if it’s anonymous:

    var a = function(message) { ... }; a(message);
    

    Since you’re passing it in the location bar, you’ll have to URI encode it, etc. but the approach should work.

    Update:

    Here’s something like what I mean:

    js = "javascript: var a=" + callbackSuccess + ";a(message);";
    String url = "javascript:" + Uri.encode(js);
    webView.loadUrl(url);
    

    Now you can give it something like “function(message) { alert(message); }” and it’ll work. I’d be pretty careful about letting the user run arbitrary code here by the way. Hard to say without seeing more, but this kind of thing can risk exposing security holes fairly easily.

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

Sidebar

Related Questions

I have to use a class/assembly made in C# .NET from native C++ application.
i have a native win32 c++ application which has a checkbox in it. I
I'm creating an application which has a home screen that looks like the native
I have ipad application which has 3 tableviews with holds data like label,image,property lists
I have a relatively simple application which I need to make native Mac OSX
Background: I have an application written in native C++ which uses the wxWidgets toolkit's
I have been working on facebook application and you know on facebook native javascript
I have an application written using Prism 4.0. It has lots of Views which
I have an network application which uses a select loop like this: bool shutdown=false;
I have to call some native C functions from C# using p/invoke. So far,

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.