简体   繁体   中英

Sending value from java to javascript using JavaScriptInterface

Android App has:

JavaScriptNFCInterface NFCIDConnector = new JavaScriptNFCInterface(this);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.addJavascriptInterface(NFCIDConnector, "Android");

public class JavaScriptNFCInterface {
    public JavaScriptNFCInterface(Context c) {
        mContext = c;
    }
        @JavascriptInterface
        public String getNFCID() {
            return NFCID;
        }

}

And for testing purpose in UI:

<a class="btn btn-primary" href="#" onclick="console.log(Android.getNFCID)" role="button">test</a>

but im receiving only:

chromium﹕ [INFO:CONSOLE(1)] "function () { [native code] }", source: http://192.168.173.216:8082/#/tablet/index (1)

not string with ID.

You are missing parentheses after the function name, so instead of calling it, you are just printing it. You need to replace:

console.log(Android.getNFCID)

with:

console.log(Android.getNFCID())

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