Entity view (Content)

How to access functions a user created in Android from Sproutcore without Phonegap Plugins

By jkim
Oct. 19, 2011

Phonegap is based on HTML5 and Javascript, and allows you to get access APIs of mobile devices. Phonegap API doc shows enable mobile device APIs, such as Acceleromter, Camera, and so on. http://docs.phonegap.com/en/1.1.0/index.html However, if you are planning to make an Android application with integrating Sproutcore and phonegap, you definitely want to get access functions that you create in Android. Phonegap supports plugins to enable this feature for both iOS and Android. http://wiki.phonegap.com/w/page/36753496/How%20to%20Create%20a%20PhoneGap%20Plugin%20for%20iOS

However, this can be easily implemented on Android because it supports Javascriptinterface API which is addJavascriptInterface().

First, you need to create a class with methods you want to use: public class MyClass {   private WebView mAppView;   private DroidGap mGap;   public MyClass(DroidGap gap, WebView view)   {     mAppView = view;     mGap = gap;     }   // function that will be called from Sproutcore   public void My_function(){     //implement your own function   } }

Next, you need to add this class as a javascriptinterface public class Main extends DroidGap {   private MyClass mc;   @Override   public void onCreate(Bundle savedInstanceState)   {     super.onCreate(savedInstanceState);     mc = new MyClass(this, appView);     appView.addJavascriptInterface(mc, "MyCls");     super.loadUrl(getString(R.string.url));   } }

Now use window.MyCls.My_function() to call My_function() that you created in Android. //in Sproutcore triggerFunction: function(){   window.MyCls.My_function(); } Isn't it simple? I have implemented this for Android projects and it works just fine. Please do not hesitate to apply this towards your Android project. It will call functions that you create in Android projects.

Post Tags: