通过 ANE(Adobe Native Extension) 启动Andriod服务 推送消息(三)

49 篇文章 0 订阅
37 篇文章 0 订阅


http://www.cnblogs.com/bobolive/p/3203040.html

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

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

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

复制代码
<extension xmlns="http://ns.adobe.com/air/extension/3.1">
    <id>nav.wenbo.service</id>
<versionNumber>0.0.1</versionNumber>
<platforms>
<platform name="Android-ARM">
<applicationDeployment>
<nativeLibrary>libAndroidServiceLib.jar</nativeLibrary>
    <initializer>nav.wenbo.service.ServiceExtension</initializer>
</applicationDeployment>
</platform>
<platform name="default">
<applicationDeployment/>
</platform>
 </platforms>
</extension>
复制代码

这里我只配置了Andriod平台,配置的平台都必须在ANE包结构中有对应的目录,如需支持其它平台如iPhone,要加上如下配置

复制代码
<platform name="iPhone-ARM">
<applicationDeployment>
<nativeLibrary>XXXLib.a</nativeLibrary>
<initializer>XXXExtensionInitializer</initializer>
<finalizer>XXXExtensionFinalizer</finalizer>
</applicationDeployment>
</platform>
复制代码

 接下来,创建ServiceController, 首先是要获取到扩展的上下文.

复制代码
        private var extContext:ExtensionContext;
        public function ServiceController()
        {
            super();
            extContext = ExtensionContext.createExtensionContext( "nav.wenbo.service", "" );//注意这个是要和extensiion里的id对应
            
            if ( !extContext ) {
                throw new Error( "service native extension is not supported on this platform." );
            }
        }
复制代码

当然,大部分时候会需要本地扩展返回的消息,这里加个监听

extContext.addEventListener( StatusEvent.STATUS, onStatus );

那么,就可以通过这个上下文来调用jar包中的方法。

复制代码
        public function setNotification($msg:String="hei~ here is a test from wenbo!"):void
        {
            extContext.call( "send", $msg );
        }
        
        private function init():void {
            extContext.call( "init" );
        }
        
        public function startAndriodService():void
        {
            extContext.call( "service", true);
        }
        
        public function stopAndriodService():void
        {
            extContext.call( "service", false);
        }
复制代码

完整代码:

package nav.wenbo.service
{
    import flash.events.EventDispatcher;
    import flash.events.StatusEvent;
    import flash.external.ExtensionContext;
    
    public class ServiceController extends EventDispatcher
    {
        private static var _instance:ServiceController;
        private var extContext:ExtensionContext;
        public function ServiceController()
        {
            super();
            extContext = ExtensionContext.createExtensionContext( "nav.wenbo.service", "" );//注意这个是要和extensiion里的id对应
            
            if ( !extContext ) {
                throw new Error( "service native extension is not supported on this platform." );
            }
            
            extContext.addEventListener( StatusEvent.STATUS, onStatus );
            init();
        }
        
        protected function onStatus(event:StatusEvent):void
        {
            var lev:String = event.level;
            trace("status change");
        }
        
        public static function get instance():ServiceController
        {
            if(null == _instance) _instance = new ServiceController;
            return _instance;
        }
        
        public function setNotification($msg:String="hei~ here is a test from wenbo!"):void
        {
            extContext.call( "send", $msg );
        }
        
        private function init():void {
            extContext.call( "init" );
        }
        
        public function startAndriodService():void
        {
            extContext.call( "service", true);
        }
        
        public function stopAndriodService():void
        {
            extContext.call( "service", false);
        }
    }
}
View Code

在ServiceLibDefault,直接把ServiceController拷过去,把所有外部引用去掉就可以了。

package nav.wenbo.service
{
    import flash.events.EventDispatcher;
    
    public class ServiceController extends EventDispatcher
    {
        private static var _instance:ServiceController;
        public function ServiceController()
        {
            super();
        }
        
        public static function get instance():ServiceController
        {
            if(null == _instance) _instance = new ServiceController;
            return _instance;
        }
        
        public function setNotification($msg:String="hei~ here is a test from wenbo!"):void
        {
            trace( "send", $msg );
        }
        
        private function init():void {
            trace( "init" );
        }
        
        public function startAndriodService():void
        {
            trace( "service", true);
        }
        
        public function stopAndriodService():void
        {
            trace( "service", false);
        }
    }
}
View Code

下一节,我会介绍把前三节的工作打包成一个ANE包供项目调用。

p: 更多的技术交流 wenbocode@126.com

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值