Android Question WebviewExtras2 AddJavascriptInterface Not working

khaleel

Member
Licensed User
Hello every one.
in the folloowing code I use WebviewExtras2.AddJavascriptInterface to call a sub from B4a .
but it never works , and I cant figure out the problem with this code and why the 'MyFunction' never gets reached

Also I'm not sure about the first parameter to the 'AddJavascriptInterface ' should it be the webview instance or the B4A Module ?

Any help would be appreciated .

Code:

#Region Project Attributes
#ApplicationLabel: Golden Van Sales
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: portrait
#CanInstallToExternalStorage: False
#End Region

#Region Activity Attributes
#FullScreen: true
#IncludeTitle: false
#End Region

Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.



End Sub

Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim wve As WebViewExtras
Private WebView1 As WebView

End Sub

Sub Activity_Create(FirstTime As Boolean)

If FirstTime Then
End If
Dim wc As DefaultWebChromeClient
WebView1.Initialize("WebView1")
wc.Initialize("wc1")

wve.Initialize(WebView1)
wve.SetWebChromeClient( wc)
Activity.AddView(WebView1,0,0,90%x,90%y)

WebView1.LoadHtml("<html><body>Hello world! <button onclick=" & Chr(34) & "B4A.CallSub('MyFunction',true);" & Chr(34) & ">KOKO</button> </body></html>")

End Sub



Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub




Sub MyFunction(x As String)
Msgbox("It Works",x)
End Sub

Sub WebView1_PageFinished (Url As String)
WebView1.JavaScriptEnabled=True
wve.JavaScriptEnabled=True
'wve.AddJavascriptInterface(Me ,"B4A")
wve.AddJavascriptInterface(WebView1 ,"B4A")
End Sub
 

khaleel

Member
Licensed User
Please use [CODE]code here...[/CODE] tags when posting code.

this line should be in activity_create (before you load any html)
Thanks for your fast reply,Manfred .
I've change the code like you suggested but with no luck
B4X:
#Region  Project Attributes
    #ApplicationLabel: Golden Van Sales
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region
#Region  Activity Attributes
    #FullScreen: true
    #IncludeTitle: false
#End Region
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
     
End Sub
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim wve As WebViewExtras
    Private WebView1 As WebView
End Sub
Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
    End If
    Dim wc As DefaultWebChromeClient
 
    WebView1.Initialize("WebView1")
    wve.Initialize(WebView1)
    wc.Initialize("wc1")
 
    WebView1.JavaScriptEnabled=True
    wve.JavaScriptEnabled=True
 
 
 
    wve.SetWebChromeClient( wc)
    Activity.AddView(WebView1,0,0,90%x,90%y)
    wve.AddJavascriptInterface(Me ,"B4A")
    'wve.AddJavascriptInterface(WebView1 ,"B4A")
   
    wve.LoadHtml("<html><body>Hello world! <button onclick=" & Chr(34) & "B4A.CallSub('MyFunction',true,'****');" & Chr(34) & ">KOKO</button>  </body></html>")
End Sub
Sub Activity_Resume
 
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub MyFunction(x As String)
    Msgbox("It Works",x)
End Sub
Sub WebView1_PageFinished (Url As String)
 
End Sub
 
Last edited:

khaleel

Member
Licensed User
and what about the first parameter to the AddJavascriptInterface ? Can you confirm it to be the WebView instance or the B4A module ?
 

khaleel

Member
Licensed User
Can any one post a little example that a javascript function calls a B4A sub passing some string data ?
I'm using WebViewExtras2
 
Top