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

Web and WebWorks Development

Reply
Developer
huddarveena
Posts: 103
Registered: ‎12-12-2011
My Device: Developer
Accepted Solution

Call java class from JavaScript file in native blackberry development.

Hello all,

          I have a .js file, which am loading in the BrowserField. My requirement is to call the .java file(java class) from the .js file to get the control back to java. I am trying to display a BB java screen on clicking on a component in .js file. Please help me to get the solution.

Retired
astanley
Posts: 1,382
Registered: ‎07-02-2009
My Device: BlackBerry Bold 9900
My Carrier: Bell

Re: Call java class from JavaScript file in native blackberry development.

What you are looking for is the ScriptableFunction class.

 

You can extend the script engine by mapping a JavaScript object name to a ScriptableFunction Java class.  In this example, if you called foo.Bar() from JavaScript, the BrowserField would facilitate a call to the invoke() method defined within the MyScriptableFunction() class:

 

protected BrowserField _browserField	= null;
_browserField = new BrowserField();
_browserField.extendScriptEngine("foo.Bar", new MyScriptableFunction());

 

 

Then, you define your ScriptableFunction class like this, and fill it with whatever Java code you need.  Note: it will only accept strings as input parameters.

 

 

import net.rim.device.api.script.ScriptableFunction;

/**
 * @description Demonstration of using the ScriptableFunction class added to version 5.0 of the BlackBerry Device
 *              Software
 * @version 1.0
 * @author Adam Stanley, Developer Relations
 * @category BlackBerry BrowserField Development: ScriptableFunction API
 * @see http ://www.blackberry.com/developers/docs/5.0.0api/net/rim/device/api/script/ScriptableFunction .html
 */

/**
 * This class can be used to represent a function in the JavaScript environment.
 */
public class MyScriptableFunction extends ScriptableFunction
{

	/**
	 * User has invoked this ScriptableFunction from the JavaScript engine This example shows how to open the Messages
	 * application and create a new message
	 */
	public Object invoke(Object obj, Object[] args) throws Exception
	{
		if (args.length == 3)
		{

			String param1 = args[0].toString();
			String param2 = args[1].toString();
			String param3 = args[2].toString();

			//Do something
		}
		return Boolean.TRUE; //Always
	}

}

 

 

Hope that helps.

Follow me on Twitter: @n_adam_stanley
-------------------------------------------------------------------------------------------------------------------------
Your app doesn't work? Use BlackBerry remote web inspector to find out why.
Developer
huddarveena
Posts: 103
Registered: ‎12-12-2011
My Device: Developer

Re: Call java class from JavaScript file in native blackberry development.

Thank u so much for the answer @astanley. It worked and I got the solution. Thanks again.