myJSNamespace.CallSub("myB4ASubName");
myJSNamespace.CallSub("myB4ASubName", "param 1");
myJSNamespace.CallSub("myB4ASubName", "param 1", "param 2");
public void execJS(WebView aWebView, String jsStatement) {
jsStatement = "javascript:" + jsStatement;
aWebView.loadUrl(jsStatement);
}
public class JSInterface {
private BA activity;
public void Initialize(BA aBA, WebView aWebView, String jsNamespace) {
activity = aBA;
aWebView.addJavascriptInterface(this, jsNamespace);
}
// rest of source omitted
public String CallSub(String subName, String param1, Float param2) {
subName = subName.toLowerCase();
Object[] params = { param1, param2 };
return (String) interfaceBA.raiseEvent(this, subName, params);
}
Sub setStoreData(property As String, value As Float)
' code here
End Sub
public String WhatType(Integer myInt){
String str="int";
return (String) interfaceBA.raiseEvent(this, "debugit", str);
}
public String WhatType(Float myFloat){
String str="float";
return (String) interfaceBA.raiseEvent(this, "debugit", str);
}
public String WhatType(Long myLong){
String str="long";
return (String) interfaceBA.raiseEvent(this, "debugit", str);
}
Sub debugit(str As String)
Log(str)
End Sub
B4A.WhatType(1);
B4A.WhatType(23.45);
B4A.WhatType(Math.pow(23,40);
No, I don't think so. As long as you Initialize each WebView individually it should be OK as far as I can see. However, as each WebView runs Javascript on its own thread if twoWebViews try to call the same Sub you might get cross thread variable corruption problems. The CallSubs themselves are thread-safe as they hold only a local variable but if the called Subs access Globals then there might be a problem.What if you have more than one WebView in a single activity and you want a JSInterface added to each?
Is an instance of JSInterface required for each WebView?
That's puzzling! I can't see anything wrong there. I'd put a Log in each CallSub variant to see that Javascript really is calling the String-Float variant and not the String-String one.The params object causing a signature mismatch when it tries to call my B4A Sub:
Log.i("B4A", "Callsub(S,F)");
public static void addConsoleInterface(WebView aWebView) {
aWebView.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onConsoleMessage(ConsoleMessage aConsoleMessage) {
String logMessage = aConsoleMessage.message() + " in " + aConsoleMessage.sourceId() + " (Line: " + aConsoleMessage.lineNumber() + ")";
if (aConsoleMessage.messageLevel() == ConsoleMessage.MessageLevel.DEBUG) {
Log.d("B4A", logMessage);
} else if (aConsoleMessage.messageLevel() == ConsoleMessage.MessageLevel.ERROR) {
Log.e("B4A", logMessage);
} else if (aConsoleMessage.messageLevel() == ConsoleMessage.MessageLevel.LOG) {
Log.i("B4A", logMessage);
} else if (aConsoleMessage.messageLevel() == ConsoleMessage.MessageLevel.TIP) {
Log.v("B4A", logMessage);
} else if (aConsoleMessage.messageLevel() == ConsoleMessage.MessageLevel.WARNING) {
Log.w("B4A", logMessage);
} else {
// log all other message types with Log.i (info)
// this condition currently will never occur but is included
// for future compatibility
Log.i("B4A", logMessage);
}
return true;
}
});
}
myJSNamespace.CallSub("myB4ASubName", "param 1", "param 2", "param 3");
Yes, It's really great. Now I can use google maps Api 3 with markers placed in Webview with real live position tracking while driving/moving.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?