FlashRuntimeExtensions.jar位于FB的目录如下:
..\Adobe Flash Builder 4.6\sdks\4.6.0\lib\android\
FREExtension (Implements)
FREContext (extends)
FREFunction (Implements)
ToastFunction
注意在第一次新建类的时候把包名填上
call方法是最终调用到Android native的地方,第二个参数是从Flex手机项目(AS3)传过来的参数数组
例如,如果传过来的第一个参数是String:
第一个参数可以获取Android运行的Context
最终的代码:
package com.example.anenativelib; import android.widget.Toast; import com.adobe.fre.FREContext; import com.adobe.fre.FREFunction; import com.adobe.fre.FREInvalidObjectException; import com.adobe.fre.FREObject; import com.adobe.fre.FRETypeMismatchException; import com.adobe.fre.FREWrongThreadException; public class ToastFunction implements FREFunction { @Override public FREObject call(FREContext context, FREObject[] params) { String msg = null; try { msg = params[0].getAsString(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (FRETypeMismatchException e) { e.printStackTrace(); } catch (FREInvalidObjectException e) { e.printStackTrace(); } catch (FREWrongThreadException e) { e.printStackTrace(); } Toast.makeText(context.getActivity(), msg, Toast.LENGTH_SHORT).show(); return null; } }
package com.example.anenativelib; import java.util.HashMap; import java.util.Map; import com.adobe.fre.FREContext; import com.adobe.fre.FREFunction; public class NativeContext extends FREContext { private static final String TOAST_FUNC_KEY = "toast"; @Override public void dispose() { } @Override public Map<String, FREFunction> getFunctions() { Map<String,FREFunction> functions = new HashMap<String, FREFunction>(); functions.put(TOAST_FUNC_KEY, new ToastFunction()); return functions; } }
package com.example.anenativelib; import com.adobe.fre.FREContext; import com.adobe.fre.FREExtension; public class NativeExtension implements FREExtension { @Override public FREContext createContext(String arg0) { return new NativeContext(); } @Override public void dispose() { } @Override public void initialize() { } }
package com.example.aslib { import flash.external.ExtensionContext; public class AndroidToast { public var extCtx:ExtensionContext=null; public function AndroidToast() { extCtx = ExtensionContext.createExtensionContext("com.example.myextension",""); } public function showToast(msg:String):void{ if(extCtx){ extCtx.call("toast",msg); } } } }
<extension xmlns="http://ns.adobe.com/air/extension/3.1"> <id>com.example.myextension</id> <versionNumber>1</versionNumber> <platforms> <platform name="Android-ARM"> <applicationDeployment> <nativeLibrary>nativelib.jar</nativeLibrary> <initializer>com.example.anenativelib.NativeExtension</initializer> </applicationDeployment> </platform> </platforms> </extension>
打钩后,应用,确定