AIR Native Extentions for Android ( ActionScript 部分 ) |
|
AndroidExtensions.as
package lightbox
{
import flash.events.EventDispatcher;
import flash.events.StatusEvent;
import flash.external.ExtensionContext;
import flash.system.Capabilities;
/**
* This class defines a simple AIR Native Extension exposing a set of simple
* and useful Android native facilities:
*
* <li>sharing (image or text)</li>
* <li>SMS sending</li>
* <li>Toast messages</li>
*
*
* @author mr_archano (twitter: mr_archano)
*
*/
public class AndroidExtensions
{
/////////////////////////////////////////
// Static fields
//
protected static var _dispatcher:EventDispatcher = new EventDispatcher();
protected static var _context:ExtensionContext = null;
/**
* Used to prompt an Android Toast message on the screen.
* @param text the text to be shown.
* @param showForLong set the duration of the toast.
* If true, the value <code>Toast.LENGTH_LONG</code> is used.
* <code>Toast.LENGTH_SHORT</code> otherwise.
*
*/
public static function toast(text:String, showForLong:Boolean = false) : void
{
if( isSupported )
context.call("toast", text, showForLong);
else
trace( "DEBUG: called AndroidExtensions.toast\n" +
"- text: "+text+"\n" +
"- showForLong: "+showForLong+"\n");
}
public static function openURL(text:String) : void
{
if( isSupported )
context.call("openURL", text);
else
trace( "DEBUG: called AndroidExtensions.toast\n" +
"- text: "+text);
}
public static function getVersion() : String
{
if( isSupported )
return context.call("getVersion") as String;
else
trace( "DEBUG: called AndroidExtensions.GetVersion");
return "not Supported";
}
/**
* Used to dispose the extension.
*
*/
public static function dispose() : void
{
if(_context)
{
_context.removeEventListener(StatusEvent.STATUS, onStatusEventHandler);
_context.dispose();
}
_context = null;
}
/////////////////////////////////////////
// Event handling functions
//
public static function addEventListener(type:String, listener:Function) : void
{
_dispatcher.addEventListener(type, listener);
}
public static function removeEventListener(type:String, listener:Function) : void
{
_dispatcher.removeEventListener(type,listener);
}
/////////////////////////////////////////
// Protected functions
//
protected static function get context() : ExtensionContext
{
if(!_context)
{
_context = ExtensionContext.createExtensionContext("lightbox.ane.androidextensions","");
_context.addEventListener(StatusEvent.STATUS, onStatusEventHandler);
}
return _context;
}
protected static function get isSupported() : Boolean
{
return (Capabilities.os.indexOf("Linux") >= 0);
}
/////////////////////////////////////////
// Callbacks
//
protected static function onStatusEventHandler(event:StatusEvent) : void
{
if(event == null || event.level == null)
return;
// debug
trace("[AndroidExtensions.onStatusHandler] level: "+event.level);
// handle StatusEvent
switch(event.level)
{
case AndroidExtensionsEvent.TYPE_ERROR:
onError();
break;
/* TODO: add cases here ... */
}
}
protected static function onError() : void
{
_dispatcher.dispatchEvent(new AndroidExtensionsEvent(AndroidExtensionsEvent.TYPE_ERROR));
}
}
}
AndroidExtensionsEvent.as
package lightbox
{
import flash.events.Event;
public class AndroidExtensionsEvent extends Event
{
public static const TYPE_ERROR:String = "error";
public function AndroidExtensionsEvent(type:String, bubbles:Boolean=true, cancelable:Boolean=false)
{
super(type, bubbles, cancelable);
}
override public function clone():Event
{
return new AndroidExtensionsEvent(this.type, this.bubbles, this.cancelable);
}
}
}
extension.xml
<extension xmlns="http://ns.adobe.com/air/extension/3.1">
<id>lightbox.ane.androidextensions</id>
<versionNumber>1.1.0</versionNumber>
<platforms>
<platform name="Android-ARM">
<applicationDeployment>
<nativeLibrary>ANElib.jar</nativeLibrary>
<initializer>lightbox.AndroidExtensions</initializer>
<finalizer>lightbox.AndroidExtensions</finalizer>
</applicationDeployment>
</platform>
</platforms>
</extension>
|
|