Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Contributor
Posts: 4
Registered: ‎08-03-2010
My Device: Not Specified

Is it possible to call a Java fuction from html page and getting the result back in java class?

Hi All,

 

I am new to BB development.But have checked most of sample apps in BB.

I want  to have a " two way communication between my application and the web content".

I will load index.html, using BrowserField --- and in this index.html i want to call a java fucntion written in a class which extedns UIApplication, which in turn will call a javascript function mentioned in index.html. IS it possible in bb?

ex-- in my index.html

i will have an icon and on click of that i would wan to call "loadFromJavaclass" method, which i would write in my class which extends Uiapplication class.

And in my "loadFromJavaclass" method, i would call a javascript fucntion written in index.html .

Is this possible?

Kindly Help.

Developer
Posts: 49
Registered: ‎06-02-2009
My Device: Curve8530, Curve8900, Storm9500
My Carrier: Airtel, vodaphone, Wifi

Re: Is it possible to call a Java fuction from html page and getting the result back in java class?

// use the following code example to call javascript function from java method

 

import net.rim.device.api.browser.field2.*;
import net.rim.device.api.script.ScriptEngine;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.container.MainScreen;


public class CustomBrowserScreen extends MainScreen implements FieldChangeListener
{
    
    private BrowserField _bf2;

    public CustomBrowserScreen()
    {

        String htmlContent="<html>" +
        "<script language='JavaScript'> function myfun() { alert('my fun call'); return false; } </script>" +               "<body>this is test html<b>bold text </b>" +
        " <a href='' onclick='return myfun()'>link</a>" +
        " <div id='label1'></div>" +
        "</body></html>";
        

        // click on button field java script function will call
        ButtonField bf = new ButtonField("click here");
        bf.setChangeListener(this);
        add(bf);

        BrowserFieldConfig _bfConfig = new BrowserFieldConfig();        
        _bfConfig.setProperty(BrowserFieldConfig.NAVIGATION_MODE,     BrowserFieldConfig.NAVIGATION_MODE_POINTER);
        _bfConfig.setProperty( BrowserFieldConfig.JAVASCRIPT_ENABLED, Boolean.TRUE );
        
        _bf2 = new BrowserField(_bfConfig);
        add(_bf2);
        _bf2.displayContent(htmlContent,    "http://localhost");
        
    }

    public void fieldChanged(Field arg0, int arg1) {

        String script=" myfun(); "; 

 

         /*

        String script=" function test5(){  " +
        " var one1=7; " +
        " document.getElementById('label1').innerHTML='test5'; " +
        "}  test5(); ";
        */   

 

        // run javascript from code
        ScriptEngine se =_bf2.getScriptEngine();
        
        Object retVal = (Object) se.executeCompiledScript(se.compileScript(script), null);        
               
    }

Developer
Posts: 49
Registered: ‎06-02-2009
My Device: Curve8530, Curve8900, Storm9500
My Carrier: Airtel, vodaphone, Wifi

Re: Is it possible to call a Java fuction from html page and getting the result back in java class?

// use the following code example to call java function from javascript

 

import net.rim.device.api.browser.field2.*;
import net.rim.device.api.script.ScriptableFunction;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.container.MainScreen;

 

 

public class CustomBrowserScreen1 extends MainScreen
{
    
    private BrowserField _bf2;

    public CustomBrowserScreen1()
    {

        String htmlContent="<html>" +
        "<script language='JavaScript'> function myfun() { alert('my fun will invoke java function'); " +
        " interesting.test(); return false; } </script>" +            // calling java function        
        "<body>this is test html<b>bold text </b>" +
        " <a href='' onclick='return myfun()'>link</a>" +
        " <div id='label1'></div>" +
        "</body></html>";

        BrowserFieldConfig _bfConfig = new BrowserFieldConfig();        
        _bfConfig.setProperty(BrowserFieldConfig.NAVIGATION_MODE,     BrowserFieldConfig.NAVIGATION_MODE_POINTER);
        _bfConfig.setProperty( BrowserFieldConfig.JAVASCRIPT_ENABLED, Boolean.TRUE );
        
        _bf2 = new BrowserField(_bfConfig);
        add(_bf2);
        _bf2.displayContent(htmlContent,    "http://localhost");

        
        try{
            
        // Java function,  which call from javascript
        _bf2.extendScriptEngine("interesting.test",
                new ScriptableFunction() {
                    public Object invoke(Object thiz, Object[] args)
                    {
                        UiApplication.getUiApplication().invokeLater(new Runnable() {
                            public void run() {
                                Dialog.alert("Java function call from javascript");
                            }
                    });
                    return "";
                }
            });
        }catch(Exception e){
            System.out.println("Exception interesting.test : " + e);
        }
    }
}

 

 

// please see the documentaion also

http://www.blackberry.com/developers/docs/5.0.0api/net/rim/device/api/browser/field2/BrowserField.ht...

New Contributor
Posts: 3
Registered: ‎11-19-2010
My Device: Not Specified

Re: Is it possible to call a Java fuction from html page and getting the result back in java class?

//well, now add a callback

 

import net.rim.device.api.browser.field2.*;
import net.rim.device.api.script.ScriptableFunction;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.container.MainScreen;

 

 

public class CustomBrowserScreen1 extends MainScreen

    
    private BrowserField _bf2;

    public CustomBrowserScreen1()
    {

        String htmlContent="<html>" +
        "<script language='JavaScript'> function myfun() { alert('my fun will invoke java function'); " +
        " interesting.test(); return false; }  function message2(m){alert(m);}</script>" +            // calling java function        
        "<body>this is test html<b>bold text </b>" +
        " <a href='' onclick='return myfun()'>link</a>" +
        " <div id='label1'></div>" +
        "</body></html>";

        BrowserFieldConfig _bfConfig = new BrowserFieldConfig();        
        _bfConfig.setProperty(BrowserFieldConfig.NAVIGATION_MODE,     BrowserFieldConfig.NAVIGATION_MODE_POINTER);
        _bfConfig.setProperty( BrowserFieldConfig.JAVASCRIPT_ENABLED, Boolean.TRUE );
        
        _bf2 = new BrowserField(_bfConfig);
        add(_bf2);
        _bf2.displayContent(htmlContent,    "http://localhost");

        
        try{
            
        // Java function,  which call from javascript
        _bf2.extendScriptEngine("interesting.test", 
                new ScriptableFunction() { 
                    public Object invoke(Object thiz, Object[] args) 
                    { 
                        UiApplication.getUiApplication().invokeLater(new Runnable() {
                            public void run() {
                                Dialog.alert("Java function call from javascript");

//Method 1: injected code

_bf2.executeScript("function message(m){alert(m);}; message('hello'); ");

//Method 2:call existent code

_bf2.executeScript("message2('hello'); ");
                            }
                    });
                    return "";
                }
            });
        }catch(Exception e){
            System.out.println("Exception interesting.test : " + e);
        }
    }
}

Regular Visitor
Posts: 1
Registered: ‎08-25-2011
My Device: Blackberry Torch
My Carrier: Orange

Re: Is it possible to call a Java fuction from html page and getting the result back in java class?

[ Edited ]

Hi leonelhs and rakeshsoni, thanks for your code. I'm trying to do the call the java function from a javascript and I use your code as reference.

 

Anyone has any idea if I want to include an argument in the function, for example I want the javascript function to be:

 

interesting.test(messageToBeShown);

 

so that my Java function will do

 

Dialog.alert(messageToBeShown);

 

Thank you in advance

New Contributor
Posts: 8
Registered: ‎01-03-2010
My Device: Not Specified

Re: Is it possible to call a Java fuction from html page and getting the result back in java class?

The below example will show you how to pass parameters through the javascript function to java function.

 

public class CustomBrowserScreen1 extends MainScreen

    
    private BrowserField _bf2;

    public CustomBrowserScreen1()
    {

        String htmlContent="<html>" +
        "<script language='JavaScript'> function myfun() { alert('my fun will invoke java function'); " +
        " interesting.test("argument1", "argument2"); return false; }  function message2(m){alert(m);}</script>" +            // calling java function        
        "<body>this is test html<b>bold text </b>" +
        " <a href='' onclick='return myfun()'>link</a>" +
        " <div id='label1'></div>" +
        "</body></html>";

        BrowserFieldConfig _bfConfig = new BrowserFieldConfig();        
        _bfConfig.setProperty(BrowserFieldConfig.NAVIGATION_MODE,     BrowserFieldConfig.NAVIGATION_MODE_POINTER);
        _bfConfig.setProperty( BrowserFieldConfig.JAVASCRIPT_ENABLED, Boolean.TRUE );
        
        _bf2 = new BrowserField(_bfConfig);
        add(_bf2);
        _bf2.displayContent(htmlContent,    "http://localhost");

        
        try{
            
        // Java function,  which call from javascript
        _bf2.extendScriptEngine("interesting.test", 
                new ScriptableFunction() { 
                    public Object invoke(Object thiz, Object[] args) 
                    { 
                        UiApplication.getUiApplication().invokeLater(new Runnable() {
                            public void run() {

 

                            for(int i = 0; i < args.length; i++){
                                  Dialog.alert(String.valueOf(args[i]));
                            }

 

                                Dialog.alert("Java function call from javascript");

//Method 1: injected code

_bf2.executeScript("function message(m){alert(m);}; message('hello'); ");

//Method 2:call existent code

_bf2.executeScript("message2('hello'); ");
                            }
                    });
                    return "";
                }
            });
        }catch(Exception e){
            System.out.println("Exception interesting.test : " + e);
        }
    }
}

Highlighted
Super Contributor
Posts: 273
Registered: ‎11-23-2010
My Device: 9800 torch
My Carrier: Software Programmer

Re: Is it possible to call a Java fuction from html page and getting the result back in java class?

Hi There,

 

u got the correct answer please post it i am also having the same problem while calling the javascript.