Back

Titanium 进阶:在创建module的时候,如何加入参数,如何调用module中的普通方法以及它们( onCreate, proxy 构建函数 和普通方法)的调用顺序

发布时间: 2016-01-11 10:48:00

refer to:  https://developer.appcelerator.com/question/163769/android-module-creation-pass-arguments-at-creation-time

很简单。例子:

// your proxy.java:

  @Kroll.method
  public void create_marker(Map options){
    Log.i(LCAT, "== in create_marker, options: " + options.toString() );
  }

然后,我们就可以调用了:

// in app.js

proxy = require('your.module').createYourProxy();
proxy.create_marker({
  name: 'my marker'
});

需要注意的是,这里有个 调用方法的顺序:

1. create Proxy 构建函数

2. 调用各种普通方法

3. 调用  createView 方法

所以,可以看到,调用 createView()方法是放到了普通方法的后面。 如果普通方法的一些变量需要在 createView 中初始化的话,就一定要先保证该变量是初始化过的,或者,让普通方法延迟几秒执行(setTimeout )

[INFO]  TiRootActivity: (main) [0,0] checkpoint, on root activity resume. activity = com.test.tuju.Test_appActivity@26594716
[DEBUG] OpenGLRenderer: Render dirty regions requested: true
[DEBUG] Atlas: Validating map...
[INFO]  Timeline: Timeline: Activity_launch_request id:com.test.tuju time:9074122
[INFO]  MapProxy: (KrollRuntimeThread) [73,73] == in MapProxy(), com.gaodemap.MapProxy@26653367
[INFO]  ======== , 建立了proxy, 放到了win中
[INFO]  MapProxy: (main) [146,219] == in createView
[INFO]  MapProxy: (main) [167,386] == in onCreate in proxy
[INFO]  == 开始新建 marker, 看是不是延迟了3秒执行
[INFO]  MapProxy: (KrollRuntimeThread) [2708,3094] == in create_marker, options: {latitude=39.9, longitude=116.47, title=我是第一个title}

下面是完整的调用方法代码

// Proxy.java
 76   // Constructor
 77   public MapProxy()
 78   {
 79     super();
 80     Log.i(LCAT, "== in MapProxy(), " + this );
 81   }
 82 
 83   @Override
 84   public TiUIView createView(Activity activity)
 85   {
 86     Log.i(LCAT, "== in createView");

// in app.js: 
var the_module = require('com.gaodemap');

var proxy = the_module.createMap( {
  borderColor: 'green',
});

win.add(proxy);
console.info("======== , 建立了proxy, 放到了win中");

// 等proxy的createView 方法执行完之后, 3秒钟之后才开始加各种 marker
setTimeout(function(){
  console.info("== 开始新建 marker, 看是不是延迟了3秒执行");
  proxy.create_marker({
    latitude: "39.9",
    longitude: "116.47",
    title: "我是第一个title"
  })

}, 3000)

Back