简体   繁体   中英

JavascriptInterface called but not showing Toast

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.

事实证明,即使您正在安装新版本的应用程序,清除缓存也很重要。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM