Android平台的JavaScript引擎 DuktapeJava

MIT
Java C/C++ JavaScript
跨平台
2015-07-17
CodeFurtur

DuktapeJava 是针对 Android 平台封装的 Duktape  JavaScript 引擎;实现Javascript 和 Java的无缝调用。

初始化DuktapeEngnine

 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);
    duktapeEngine = new DuktapeEngine();
    duktapeEngine.put("activity",this);
    duktapeEngine.execute(AssetScript.toScript(getBaseContext(), "duk.js"));
    duktapeEngine.call("activityListener", "onCreate", savedInstanceState);
} 

@Override 
protected void onDestroy() { 
    if (duktapeEngine != null) {
        duktapeEngine.destory();
        duktapeEngine = null;
    } super.onDestroy();
}

 

duk.js javascript 代码示例

 

importClass("com.furture.react.R")
importClass("android.view.View.OnClickListener")
importClass("android.widget.Toast")
importClass("java.lang.Runnable")

var activityListener = {};
activityListener.onCreate = function(){
        print("activity onCreate onJavaScript");
        activity.setContentView(R.layout.activity_duk)
        button1 = activity.findViewById(R.id.button1);
        button1.setOnClickListener(new OnClickListener(function(){
            Toast.makeText(activity, "Button1 Clicked", Toast.LENGTH_SHORT).show(); 
            var intent = new Intent(activity, "com.furture.react.activity.DetailActivity");
            activity.startActivity(intent);
        }));

    button2 = activity.findViewById(R.id.button2);
    button2.setOnClickListener(new OnClickListener({
        onClick:function(){ 
           Toast.makeText(activity, "Button2 Clicked", Toast.LENGTH_SHORT).show();
        }
    }));
}

activityListener.onStart = function(){
    print("activity onStart");
}

activityListener.onResume = function(){ 
   print("activity onResume");
}


activityListener.onPause = function(){ 
    print("activity onPause");
}

activityListener.onStop = function(){ 
    print("activity onStop");
}

activityListener.finish = function(){ 
    print("activity finish" + num);
}

 

Java 和 Javascript 的无缝调用

javascript 调用java方法示例:

importClass("android.widget.Toast") 

Toast.makeText(activity, "Javascript toast", Toast.LENGTH_SHORT).show();

Java 还可以在 JavaScript 的对象方法示例:

importClass("com.efurture.react.DataUtils")

var data = {}; 
data.count =  10; 
data.getItem = function(var index){ 
      return "Javascript Data " + index;
} 

DataUtils.showData(data);

DataUtils.Java 代码示例:

public class DataUtils{ 
public static void showData(JSRef data){
         DuktapeEngine  engine =  data.getEngine();
         int count = (Integer)engine.call(data, "count");
          for(int i=0; i

项目github地址: https://github.com/gubaojian/DuktapeJava

加载中

评论(1)

CodeFurtur
CodeFurtur 软件作者
good work

暂无资讯

1
回答
DuktapeJava针对Android平台的嵌入式JavaScript引擎

DuktapeJava针对Android平台的嵌入式JavaScript引擎,可以实现JavaScript和Java无缝调用,看项目介绍功能很强大, 是否...

2015/10/08 14:02

没有更多内容

加载失败,请刷新页面

没有更多内容

暂无博客

返回顶部
顶部