BlackBerry 的BrowserField字段网页call back Java代码

需求:

打算用html 开发黑莓BB 7应用的导航,数据展现,

用Java开发部分页面导航,菜单,后台程序

但不用WebWorks。


这就需要从html 页面中调用JavaScript,JavaScript调用BlackBerry Java Application的业务逻辑。


例子代码如下:


package mypackage;

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

public class BrowserFieldDemo extends UiApplication
{
    public static void main(String[] args)
    {
        BrowserFieldDemo app = new BrowserFieldDemo();
        app.enterEventDispatcher();
    }

    public BrowserFieldDemo()
    {
        pushScreen(new BrowserFieldDemoScreen());
    }
}

class BrowserFieldDemoScreen extends MainScreen
{
    public BrowserFieldDemoScreen()
    {
        BrowserFieldConfig myBrowserFieldConfig = new BrowserFieldConfig();
        myBrowserFieldConfig.setProperty(BrowserFieldConfig.NAVIGATION_MODE,BrowserFieldConfig.NAVIGATION_MODE_POINTER);
        BrowserField browserField = new BrowserField(myBrowserFieldConfig);
        
        add(browserField);
        String url = "http://www.blackberry.com";
        url = "local:///index.html";
        
        try {
			browserField.extendScriptEngine("myblackberry.hello",
			        new ScriptableFunction() {
			    public Object invoke(Object thiz,final Object[] args)
			    {
			                  // here args will give you params passed by javascript                       
			                  //function
			                  // args[0] = Hello and args[1] = world
			                  //TODO do your task here...
			    	UiApplication.getUiApplication().invokeLater(new Runnable()
			        {
			            public void run()
			            {
			                Dialog.alert((String) args[0]);
			                Dialog.alert((String) args[1]);
			            }
			        });
			    	
			        return thiz;
			    }
			});
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        browserField.requestContent(url);
    }
}


<html>
<SCRIPT Language="javascript">
	function hello1(){
		alert("world 1");
	}
	function hello2(){
		myblackberry.hello("world 2", "dam you are good");
	}
</SCRIPT>

<body>
	Hello World <br/>
	<a href="JavaScript:hello1()"> click me hello world 1</a>
		<br/>
		<br/>
	<a href="JavaScript:hello2()"> click me hello world 2</a>
<body>
<html>




参考:

Passing values from a web page to your app - Android / Blackberry

http://stackoverflow.com/questions/10073810/passing-values-from-a-web-page-to-your-app-android-blackberry

你可能感兴趣的:(JavaScript,java,exception,String,object,BlackBerry)