Skip to content

ewrfedf/DuktapeJava

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

DuktapeJava

A Java NDK wrapper for duktape javascript engine on android platform, which is tiny 200-300KB so. you can use any java method in javascript by just small engine, give you endless power integrating javascript with java.

Get start

new DuktapeEngnine instance then init it.

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


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

duk.js javascript code

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);
}

###Seamless Integrating Java with Javascript

javascript can call any javamethod, and new java class instance and interface.

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

java can also call any method or get property on javascript object.

  importClass("com.efurture.react.DataUtils")
  
  var data = {};
  data.count =  10;
  data.getItem = function(index){
         return "Javascript Data " + index;
  }
  
  DataUtils.showData(data);

DataUtils Java Code Sample

  public class DataUtils{
  
     public static void showData(JSRef data){
         DuktapeEngine  engine =  data.getEngine();
         int count = (Integer)engine.callJsRef(data, "count");
         for(int i=0; i<count; i++){
             Log.d("DataUtils", " Get JavaScript Data Success :  " + engine.callJsRef(data, "getItem", i));
         }
      }
  
  }   

Reference

https://developer.mozilla.org/en-US/docs/Mozilla/Projects/Rhino/Scripting_Java

http://www.w3schools.com/js/default.asp

https://github.com/jasonsantos/luajava

About

JS 调用Android api

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 50.1%
  • C 47.7%
  • Makefile 2.0%
  • Other 0.2%