4

I have a webview that ignores all my javascript interface methods, or at least appears to. When I run the website from a normal browser, it throws errors because the interface isn't defined, as expected, but this leads me to believe that the interface methods are at least being called, but something else is wrong. Can anyone help me figure out what?

Declaring stuff

    @Override
    protected void onPostExecute(ValidationResult validation)
    {
        if (validation.wasSuccessful)
        {
            URL = validation.message;
            wv = (WebView) findViewById(R.id.webView);              
            wv.addJavascriptInterface(new KioskInterface(wv.getContext()), "Android");
            WebSettings ws = wv.getSettings();
            ws.setJavaScriptEnabled(true);
            wv.loadUrl(URL);
        }
        else
        {
            errorAlert(title, msg + "\nContact network management.");
        }
    }

Kiosk Interface class

public class KioskInterface {
    Context c;

    KioskInterface(Context context)
    {
        c = context;
    }

    @JavascriptInterface
    public void showToast()
    {
        Toast.makeText(c, "Hiding Keyboard", Toast.LENGTH_LONG).show();
    }
}

Javascript

function example() {
    Android.showToast();
}

I never see any the Toast, but I don't get any errors or broken behavior either.

erosebe
  • 927
  • 3
  • 16
  • 31
  • what if try to output to Logcat instead of showing toast? – lelloman Apr 01 '13 at 18:11
  • Thanks for the response. I'm afraid due to some peculiarities with the system at my job I cannot answer your question. Basically, I can't debug directly on my computer via USB or emulator. I have to push an apk to the device in order to test. – erosebe Apr 01 '13 at 18:23
  • ouch, I'm in pain for you...btw, does javascript interface calls occurs on UI thread? if not, you could try to show the toast from a handler – lelloman Apr 01 '13 at 18:34
  • Yeah. It sucks but this is a special case. Usually it is not like this. Tried the handler thing and nothing seems to be different. Create handler onCreate and throw the toast in to `handler.post(new Runnable(){public void run(){}})` – erosebe Apr 01 '13 at 19:06
  • Then perhaps you can try throwing an exception in the showToast method to ensure it's called (the app will crash). Besides, are you sure that "example" is called at all? Couldn't you use the webview to check that first? – Rick77 Apr 01 '13 at 19:13
  • ouch...if you posted the runnable like shown [here](http://stackoverflow.com/questions/4209814/posting-toast-message-from-a-thread) (that is not exactly what you wrote) I would ensure that both `example()` and `showToast()` get called – lelloman Apr 01 '13 at 19:27
  • I put an exception at the top of showToast and the app didn't crash. Nothing changed. So I guess you're right and it isn't getting called. I don't know what's going on, though, since in the browser when Android is undefined the website freezes, which doesn't happen in the webview. – erosebe Apr 01 '13 at 19:29
  • what does happen if you put an alert in "exaple" instead of a call to Android.showToast? Can you use the same example function to check if Android exists within the namespace and if it contains the "showToast" method (for instance by modifying some node in the DOM to print debug messages)? – Rick77 Apr 01 '13 at 19:49
  • example() does other stuff like you mention. I blur input fields and remove classes using jquery. and all the functions after the Android.showToast() call. – erosebe Apr 01 '13 at 20:01
  • OMG. I'm sorry guys. It looks like stuff was getting cached somehow, even though I was updating the apk, the website was cached and wasn't getting the new code to call the JavascriptInterface. Thank you for helping me out. Is there a best practice for answering my own question or deleting this or anything? – erosebe Apr 01 '13 at 21:52
  • Nop. I never asked a question here so I don't know if you can do this (as the poster), but I think it would be ethic in this case to delete the question, as it doesn't provide useful information. The next best solution would be closing, I guess. Glad you solved your problem btw. – Rick77 Apr 01 '13 at 22:37

1 Answers1

0

Turns out it is important to clear caches, even if you are installing new versions of the app.

erosebe
  • 927
  • 3
  • 16
  • 31