守望者--AIR技术交流

标题: 通过 ANE(Adobe Native Extension) 启动Andriod服务 推送消息(三) [打印本页]

作者: 破晓    时间: 2015-1-20 14:29
标题: 通过 ANE(Adobe Native Extension) 启动Andriod服务 推送消息(三)

jar包完成后,剩下就是要构建ANE包来供实际程序调用。

首先要建两个Flex库项目, default那个是官方建议加上的,仅用于不在真实环境下编译调试的时候有个默认接口不至于调用不成功报错,项目结构如下:


[attach]511[/attach]


首先介绍配置文件extension.xml, 这个是必须的,用于指定平台和接口会直接打到ANE包里。

  1. <extension xmlns="http://ns.adobe.com/air/extension/3.1">
  2.     <id>nav.wenbo.service</id>
  3. <versionNumber>0.0.1</versionNumber>
  4. <platforms>
  5. <platform name="Android-ARM">
  6. <applicationDeployment>
  7. <nativeLibrary>libAndroidServiceLib.jar</nativeLibrary>
  8.     <initializer>nav.wenbo.service.ServiceExtension</initializer>
  9. </applicationDeployment>
  10. </platform>
  11. <platform name="default">
  12. <applicationDeployment/>
  13. </platform>
  14. </platforms>
  15. </extension>
复制代码
这里我只配置了Andriod平台,配置的平台都必须在ANE包结构中有对应的目录,如需支持其它平台如iPhone,要加上如下配置

  1. <platform name="iPhone-ARM">
  2. <applicationDeployment>
  3. <nativeLibrary>XXXLib.a</nativeLibrary>
  4. <initializer>XXXExtensionInitializer</initializer>
  5. <finalizer>XXXExtensionFinalizer</finalizer>
  6. </applicationDeployment>
  7. </platform>
复制代码
接下来,创建ServiceController, 首先是要获取到扩展的上下文.

  1.    private var extContext:ExtensionContext;
  2.         public function ServiceController()
  3.         {
  4.             super();
  5.             extContext = ExtensionContext.createExtensionContext( "nav.wenbo.service", "" );//注意这个是要和extensiion里的id对应
  6.             
  7.             if ( !extContext ) {
  8.                 throw new Error( "service native extension is not supported on this platform." );
  9.             }
  10.         }
复制代码
当然,大部分时候会需要本地扩展返回的消息,这里加个监听

  1. extContext.addEventListener( StatusEvent.STATUS, onStatus );
复制代码
那么,就可以通过这个上下文来调用jar包中的方法。

  1. public function setNotification($msg:String="hei~ here is a test from wenbo!"):void
  2.         {
  3.             extContext.call( "send", $msg );
  4.         }
  5.         
  6.         private function init():void {
  7.             extContext.call( "init" );
  8.         }
  9.         
  10.         public function startAndriodService():void
  11.         {
  12.             extContext.call( "service", true);
  13.         }
  14.         
  15.         public function stopAndriodService():void
  16.         {
  17.             extContext.call( "service", false);
  18.         }
复制代码
完整代码:

  1. package nav.wenbo.service
  2. {
  3.     import flash.events.EventDispatcher;
  4.     import flash.events.StatusEvent;
  5.     import flash.external.ExtensionContext;
  6.    
  7.     public class ServiceController extends EventDispatcher
  8.     {
  9.         private static var _instance:ServiceController;
  10.         private var extContext:ExtensionContext;
  11.         public function ServiceController()
  12.         {
  13.             super();
  14.             extContext = ExtensionContext.createExtensionContext( "nav.wenbo.service", "" );//注意这个是要和extensiion里的id对应
  15.             
  16.             if ( !extContext ) {
  17.                 throw new Error( "service native extension is not supported on this platform." );
  18.             }
  19.             
  20.             extContext.addEventListener( StatusEvent.STATUS, onStatus );
  21.             init();
  22.         }
  23.         
  24.         protected function onStatus(event:StatusEvent):void
  25.         {
  26.             var lev:String = event.level;
  27.             trace("status change");
  28.         }
  29.         
  30.         public static function get instance():ServiceController
  31.         {
  32.             if(null == _instance) _instance = new ServiceController;
  33.             return _instance;
  34.         }
  35.         
  36.         public function setNotification($msg:String="hei~ here is a test from wenbo!"):void
  37.         {
  38.             extContext.call( "send", $msg );
  39.         }
  40.         
  41.         private function init():void {
  42.             extContext.call( "init" );
  43.         }
  44.         
  45.         public function startAndriodService():void
  46.         {
  47.             extContext.call( "service", true);
  48.         }
  49.         
  50.         public function stopAndriodService():void
  51.         {
  52.             extContext.call( "service", false);
  53.         }
  54.     }
  55. }
复制代码
在ServiceLibDefault,直接把ServiceController拷过去,把所有外部引用去掉就可以了。

  1. package nav.wenbo.service
  2. {
  3.     import flash.events.EventDispatcher;
  4.    
  5.     public class ServiceController extends EventDispatcher
  6.     {
  7.         private static var _instance:ServiceController;
  8.         public function ServiceController()
  9.         {
  10.             super();
  11.         }
  12.         
  13.         public static function get instance():ServiceController
  14.         {
  15.             if(null == _instance) _instance = new ServiceController;
  16.             return _instance;
  17.         }
  18.         
  19.         public function setNotification($msg:String="hei~ here is a test from wenbo!"):void
  20.         {
  21.             trace( "send", $msg );
  22.         }
  23.         
  24.         private function init():void {
  25.             trace( "init" );
  26.         }
  27.         
  28.         public function startAndriodService():void
  29.         {
  30.             trace( "service", true);
  31.         }
  32.         
  33.         public function stopAndriodService():void
  34.         {
  35.             trace( "service", false);
  36.         }
  37.     }
  38. }
复制代码
下一节,我会介绍把前三节的工作打包成一个ANE包供项目调用。p: 更多的技术交流 wenbocode@126.com.本文来自:http://www.cnblogs.com/bobolive/p/3203040.html



作者: 破晓    时间: 2015-1-20 14:40
本系列文章目录







欢迎光临 守望者--AIR技术交流 (http://www.airmyth.com/)