G
G
Georgy Khromchenko2014-02-11 01:39:26
Android
Georgy Khromchenko, 2014-02-11 01:39:26

Why doesn't addJavascriptInterface work in Android?

I have an Activity with a WebView

webView.getSettings().setJavaScriptEnabled(true);

 webView.addJavascriptInterface(
      new PhotoStorage(this),
      "PhotoStorage"
 );

Inside the WebView, there is code ( executed after an AJAX request with data ) that accesses PhotoStorage, and it does not work at all ( writes - Uncaught ReferenceError: PhotoStorage is not defined.)
сonsole.log(window.PhotoStorage);
console.log(PhotoStorage);
Accordingly, the question is - what did I do wrong and how to use the JSInterface inside the webview correctly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Georgy Khromchenko, 2014-02-12
@Mox

In general, I figured it out - at the first redirect, work began in a standard browser. Treated by adding your own WebView, in which the shouldOverrideUrlLoading method is overridden

@Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (
                Uri.parse(url).getHost().contains("sonyagent") ||
                (Uri.parse(url).getPort() == 3000)
            ){
                return false;
            }
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            return true;
        }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question