AIR Native Extension on Android emulator

参考
1、官网教程:http://help.adobe.com/en_US/air/extensions/index.html
2、官网教材:http://www.adobe.com/devnet/air/articles/developing-native-extensions-air.html
3、网易博客:http://sswilliam.blog.163.com/blog/#m=0&t=3&c=nativeextension

开发目标
1、在Android模拟器上运行的AIR程序
2、可以调用Native的方法
3、可以接受Native发的事件

开发准备

1、Flash Builder4.5
2、AIRSDK3

FB支持Android开发
1、安装ADT
2、设置Android的SDK目录

准备文件夹
1、总目录:HelloANE
2、存放Native的目录:Native
3、存放Flex库目录:HelloANELibrary
4、存放App目录:HelloANESample

创建Native
1、在FB中创建Android项目:
      1、新建项目时选择Android project
      2、设路径为前面的Native
      3、Build target选择3.2
      4、Package名为:com.mydomain.example.android.helloANE
2、允许该项目使用AS提供的Extension功能
      1、打开该工程属性对话框
      2、在Java Build Path中,右部选择Libraries,点击“Add External JARs”,选择FB的SDK目录中lib/android里的FlashRuntimeExtensions.jar。
3、创建Extension类      

package com.mydomain.example.android.helloANE;

import com.adobe.fre.FREContext;
import com.adobe.fre.FREExtension;

public class Extension implements FREExtension {

	@Override
	public FREContext createContext(String arg0) {
		// TODO Auto-generated method stub
		return new ExtensionContext();
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

	@Override
	public void initialize() {
		// TODO Auto-generated method stub

	}
}

4、创建ExtensionContext类      
package com.mydomain.example.android.helloANE;

import java.util.Map;
import java.util.HashMap;

import com.adobe.fre.FREContext;
import com.adobe.fre.FREFunction;

public class ExtensionContext extends FREContext {

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

	@Override
	public Map<String, FREFunction> getFunctions() {
		// TODO Auto-generated method stub
		Map<String, FREFunction> functionMap = new HashMap<String, FREFunction>();
		
		functionMap.put("initMe", new InitFunction());
		functionMap.put("isSupported", new SupportedFunction());
		functionMap.put("Work", new WorkFunction());
		
		return functionMap;
	}
}
5、创建InitFunction类      
package com.mydomain.example.android.helloANE;

import com.adobe.fre.FREContext;
import com.adobe.fre.FREFunction;
import com.adobe.fre.FREObject;

public class InitFunction implements FREFunction {

	@Override
	public FREObject call(FREContext arg0, FREObject[] arg1) {
		// TODO Auto-generated method stub
		return null;
	}

}

6、创建SupportedFunction类      
package com.mydomain.example.android.helloANE;

import com.adobe.fre.FREContext;
import com.adobe.fre.FREFunction;
import com.adobe.fre.FREObject;
import com.adobe.fre.FREWrongThreadException;

public class SupportedFunction implements FREFunction {

	@Override
	public FREObject call(FREContext arg0, FREObject[] arg1) {
		// TODO Auto-generated method stub
		FREObject result = null;
		
		try
		{
			result = FREObject.newObject(true);
		}
		catch(FREWrongThreadException e)
		{
			e.printStackTrace();
		}
		
		return result;
	}
}

7、创建Work类
package com.mydomain.example.android.helloANE;

import com.adobe.fre.FREContext;
import com.adobe.fre.FREFunction;
import com.adobe.fre.FREObject;
import com.adobe.fre.FREWrongThreadException;
import com.mydomain.example.android.helloANE.MyThread;
import android.util.*;

public class WorkFunction implements FREFunction {

	@Override
	public FREObject call(FREContext arg0, FREObject[] arg1) {
		// TODO Auto-generated method stub
		FREObject result = null;
		
		try
		{
			Log.i("WorkFunction+Call", "Enter");
			
			MyThread thread = new MyThread(arg0);
			thread.start();
			
			result = FREObject.newObject("Native");
		}
		catch(FREWrongThreadException e)
		{
			e.printStackTrace();
		}
		
		return result;
	}
}
8、创建MyThread类
package com.mydomain.example.android.helloANE;
import android.util.*;

import com.adobe.fre.*;

public class MyThread extends Thread {
	private FREContext MyExt = null;
	public MyThread(FREContext ext)
	{
		Log.i("MyThread Constructor", "Enter");
		MyExt = ext;		
	}
	
	public void run()
	{
		Log.i("MyThread run", "Enter");
		MyExt.dispatchStatusEventAsync("BACK", "");
	}
}
9、导出JAR

创建AS库
1、创建Flex Library工程,名称为HelloANELibrary,确保Include AIR
2、创建一个AS的Class
package com.mydomain.nativeExtension.Work2
{
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.external.ExtensionContext;
	import flash.events.StatusEvent;
	
	public class Work extends EventDispatcher
	{
		private static var extContext:ExtensionContext = null;
		private static var isCallBack:Boolean = false;
		public function Work(target:IEventDispatcher=null)
		{
			super(target);
			if(!extContext)
			{
				extContext = ExtensionContext.createExtensionContext("com.mydomain.example","");
				extContext.addEventListener(StatusEvent.STATUS, OnStatus);
				extContext.call("initMe", null);
			}
		}
		
		public static function get isSupported():Boolean
		{
			return extContext.call("isSupported", null) as Boolean;
		}
		
		public static function OnStatus(event:StatusEvent):void
		{
			isCallBack = true;
		}
		
		public function Action():String
		{
			if(isCallBack)
			{
				return "isCallBack is true";
			}
			else
			{
				return extContext.call("Work", null) as String;
			}
		}
	}
}


打包ANE
1、在Flex Library的bin目录下建立一个文件夹“Android-ARM”。这个文件夹用于存放针对Android的ANE文件,因为有可能一份AS库代码产生多种平台的代码。
2、提取SWF。用WINRAR打开SWC,把Library.swf提取出来,和swc放在同一目录下
3、准备extension.xml,描述native
<extension xmlns="http://ns.adobe.com/air/extension/2.5">
	<id>com.mydomain.example</id>
	<versionNumber>1</versionNumber>
	<platforms>
		<platform name="Android-ARM">
			<applicationDeployment>
				<nativeLibrary>HelloANENative.jar</nativeLibrary>
				<initializer>com.mydomain.example.android.helloANE.Extension</initializer>
			</applicationDeployment>
		</platform>
	</platforms>
</extension>

4、在bin目录下准备证书文件
5、在bin目录下准备批处理如下

set adt_dir="D:\Program Files\Adobe\Adobe Flash Builder 4.5\sdks\AIRSDK3\bin"
set sign=-storetype pkcs12 -keystore wvw.p12 -storepass geheim

%adt_dir%\adt -package %sign% -tsa none -target ane Android-ARM\Helloane.ane extension.xml -swc HelloANELibrary.swc -platform Android-ARM library.swf HelloANENative.jar
6、运行批处理,如正常,将在Android-ARM下产生ane文件

创建使用Extension的App
1、创建一个Flex Mobile工程,名称为HelloANESample,Mobile Setting选择Blank,Target为Android,第四步中通过“Add Project”加入前面的Flex库工程
2、在App描述文件中(HelloANESample-app.xml)加入对extension的描述,如下
<extensions>
		<extensionID>com.mydomain.example</extensionID>
</extensions>
3、界面代码如下
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
			   xmlns:s="library://ns.adobe.com/flex/spark">
	<fx:Script>
		<![CDATA[
			import com.mydomain.nativeExtension.Work2.Work;
			
			import mx.events.FlexEvent;
			
			private var Ext:Work = null;
			protected function button1_clickHander(event:MouseEvent):void
			{
				if(Ext == null)
				{
					Ext = new Work();
				}
				this.output.text = Ext.Action();
			}
		]]>
	</fx:Script>
	
	<fx:Declarations>
		<!-- Place non-visual elements (e.g., services, value objects) here -->
	</fx:Declarations>
	<s:Button x="370" y ="10" label="Button" click ="button1_clickHander(event)"/>
	<s:Label x="370" y ="10" width="100" id="output" text="label" />
</s:Application>

打包APK
1、把证书拷贝至App的输出目录
2、提供批处理如下
set adt_dir="D:\Program Files\Adobe\Adobe Flash Builder 4.5\sdks\AIRSDK3\bin"
set sign=-storetype pkcs12 -keystore wvw.p12 -storepass geheim

%adt_dir%\adt -package -target apk-emulator %sign% myApp.apk HelloANESample-app.xml HelloANESample.swf -extdir D:\Code\HelloANE\HelloANELibrary\bin\Android-ARM
3、成功运行会在当前目录下生成myApp.apk

在模拟器上安装运行
1、启动3.2的模拟器
2、安装apk
3、运行,第一次按按钮,界面上文字会显示Native,第二次之后则显示“isCallBack is true”。


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zz962/article/details/6873194
个人分类: Adobe
想对作者说点什么? 我来说一句

带附件发邮件

2014年09月17日 53KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭