Adobe Air - Opening a File With Air
So I've created an Air app that saves to a custom file type. I've set up the file associations when I publish the app and, when you double click the file it opens the air app. What are the hooks for me to detect that the app has been opened via a file? Obviously, I need to detect this and then get t开发者_开发百科he app to open the file itself.
Listen for the invoke
event on the WindowedApplication or its nativeApplication. It has an arguments array property that holds the string arguments passed during this invocation.
The
NativeApplication
object of an AIR application dispatches aninvoke
event when the application is invoked.The
NativeApplication
object always dispatches an invoke event when an application is launched, but the event may be dispatched at other times as well. For example, a running application dispatches an additionalInvokeEvent
when a user activates a file associated with the application.Only a single instance of a particular application can be launched. Subsequent attempts to launch the application will result in a new invoke event dispatched by the
NativeApplication
object of the running instance. It is an application's responsibility to handle this event and take the appropriate action, such as opening a new application window to display the data in a file.
InvokeEvent
objects are dispatched by theNativeApplication
object (NativeApplication.nativeApplication
). To receive invoke events, call theaddEventListener()
method of theNativeApplication
object. When an event listener registers for an invoke event, it will also receive allinvoke
events that occurred before the registration. These earlier events are dispatched after the call toaddEventListener()
returns, but not necessarily before a new invoke event that might be might be dispatched after registration. Thus, you should not rely on dispatch order.
<mx:WindowedApplication creationComplete="init()">
<mx:Script>
<![CDATA[
public function init():void
{
NativeApplication.nativeApplication.addEventListener(InvokeEvent.Invoke, onInvoke);
}
public function onInvoke(e:InvokeEvent):void
{
var args:Array = e.arguments;
trace("There are " + args.length + " arguments");
for(var i:int = 0; i < args.length; i++)
{
trace("Argument #" + i + " " + args[i]);
}
}
]]>
</mx:Script>
</mx:WindowedApplication>
Listen to the InvokeEvent which will hold into the arguments
property the file name requested:
Quick mxml example:
<?xml version="1.0"?>
<mx:WindowedApplication
xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:s="spark.components.*"
invoke="onAppInvoke(event);">
<mx:Script><![CDATA[
import mx.controls.Alert;
private function onAppInvoke(event:InvokeEvent):void {
if (event.arguments.length>0) {
// ok app call with an arguments
var fileName:String=event.arguments[0];
Alert.show("app open with : "+fileName);
} else {
// app open normally
Alert.show("normal launch");
}
}
]]></mx:Script>
</mx:WindowedApplication>
精彩评论