我创建了一个 Native Extension 以在 Android 上使用 zxing 应用程序(通过意图)。
该应用程序启动正常并扫描条形码。
我只是无法将结果返回到我的 Flex 项目中。似乎 onActivityResult 方法永远不会被本机扩展调用。
你们能帮忙吗?
下面是代码:
public class BarcodeScanFunction implements FREFunction {
public static BarcodeExtensionContext _context;
@Override
public FREObject call(FREContext context, FREObject[] passedArgs) {
_context = (BarcodeExtensionContext)context;
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
context.getActivity().startActivityForResult(intent, 0);
return null;
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult ir = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
_context.dispatchStatusEventAsync("DATA_CHANGED", ir.getContents());
}
}
Actionscript 处理程序:
public class NativeBarcode extends EventDispatcher
{
private static var extContext:ExtensionContext = null;
public function NativeBarcode(target:IEventDispatcher=null)
{
super(target);
if ( !extContext )
{
extContext = ExtensionContext.createExtensionContext("za.co.flatrocksolutions.Barcode","barcode");
extContext.call("initMe");
}
}
public static function get isSupported():Boolean
{
var toReturn:Boolean = false;
toReturn = extContext.call("isSupported") as Boolean;
return toReturn;
}
public function scan():void
{
extContext.addEventListener(StatusEvent.STATUS, onStatus);
extContext.call("scanMe");
}
public function onStatus(event:StatusEvent):void {
//dispatchEvent(event);
var e : StatusEvent = new StatusEvent("DATA_CHANGED", true, false, event.code, event.level);
dispatchEvent(e);
}
}
使用 ANE:
be = new NativeBarcode();
be.addEventListener("DATA_CHANGED", onStatus);
be.scan();
……
private function onStatus(e:StatusEvent):void
{
if (e.code == "DATA_CHANGED")
{
var stringData:String = e.level;
// ...
}
}