User Tools

Site Tools


how_to_use_the_android_framework_through_script

====== Use of the Android framework from script ====== ===== Overview ===== Thanks to the JavaScript engine, it is possible to create rather complex pieces of software. These scripts can access Lightning features (items, containers, launcher configuration, etc.) but can also leverage the full power of Android through direct framework calls. In order to use the Android framework, there are three simple things to know: * use [[http://www.pierrox.net/android/applications/lightning_launcher/script/reference/net/pierrox/lightning_launcher/script/api/LL.html#getContext%28%29|LL.getContext()]] to retrieve the current context, which is often needed to access platform services * use fully qualified class names, such as <code>android.hardware.Sensor.TYPE_ACCELEROMETER</code>, in order to access any class, or use [[http://www.pierrox.net/android/applications/lightning_launcher/script/reference/net/pierrox/lightning_launcher/script/api/LL.html#bindClass%28java.lang.String%29|LL.bindClass("android.hardware.Sensor")]] once to shorten this kind of expression to <code>Sensor.TYPE_ACCELEROMETER</code> * errors in scripts called directly from the Android framework (typically callbacks) **are not recoverable**. The error will be displayed, but the launcher will be halted, then restarted. ===== Sample code ===== This is a simple compass script. Create an item, detach it from the grid and replace the current id "0x1000a" with the actual value in your setup. <sxh javascript;> LL.bindClass("android.hardware.Sensor"); LL.bindClass("android.hardware.SensorEventListener"); LL.bindClass("android.hardware.SensorManager"); // replace the id here with your actual compass item id var COMPASS_ID = 0x1000a; var compass = LL.getCurrentDesktop().getItemById(COMPASS_ID); // retrieve the android sensor service and retrieve an orientation sensor var sm = LL.getContext().getSystemService(Context.SENSOR_SERVICE); var orientation = sm.getDefaultSensor(Sensor.TYPE_ORIENTATION); // this is the interface function definition var i = { onAccuracyChanged: function(sensor,accuracy){}, onSensorChanged: function(event) { var azimuth = event.values[0]; compass.setRotation(-azimuth); compass.setLabel(Math.round(azimuth)); } }; // this is the listener based on the interface function definition var l = new SensorEventListener(i); // register our listener, which will be called by the Android framework repeatedly sm.registerListener(l, orientation, SensorManager.SENSOR_DELAY_GAME); // critical: later, don't forget to unregister this listener to prevent rapid battery consumption // sm.unregisterListener(l); </sxh>

how_to_use_the_android_framework_through_script.txt · Last modified: 2014/09/15 07:25 by pierrox