Apache Adobe Flex TutorialTutoriaux Adobe Flex & AIR en Français

12sept/100

AIR pour Android – Ouvrir une application depuis une URI avec arguments

Dans le tutorial précédent, on a vu que l'on peut ouvrir une application depuis une URI, en suivant un certain formattage:

Air Android – Ouvrir une application Air sur Android depuis le navigateur web avec une custom URI

En plus de cela, on peut ouvrir l'application en lui passant des paramètres. Pour cela, il suffit de passer les paramètres dans l'URL en GET comme ceci:

identifianturi://arg1=value1&arg2=value2 .....

Pour la récupération, on suit le même mécanisme que pour l'ouverture d'une application Air classique. On capte l'évènement de type InvokeEvent de l'API Air:

http://livedocs.adobe.com/flex/3/langref/flash/events/InvokeEvent.html

Démonstration vidéo

Pour cette application, je vais ouvrir le navigateur web vers la page de cet article puis naviguer vers les liens suivants:

flextutorialcustomuriArg://

flextutorialcustomuriArg://arg1=value1&arg2=value2

Si vous avez un 404, assurez-vous d'avoir bien installé l'application (voir en fin de billet).

Code de l'application

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" paddingLeft="0" paddingRight="0"
                paddingTop="0" paddingBottom="0"
                applicationComplete="application1_applicationCompleteHandler(event)">
  <mx:Style>
    global {
      font-size: 24;
    }
  </mx:Style>
  <mx:Script>
    <![CDATA[
      import mx.events.FlexEvent;

      protected function application1_applicationCompleteHandler(event:FlexEvent):void {
        NativeApplication.nativeApplication.addEventListener(InvokeEvent.INVOKE, onInvoke);
      }

      private function onInvoke(invokeEvent:InvokeEvent):void {
        ta.text = "";
        var directory:String = invokeEvent.currentDirectory ? invokeEvent.currentDirectory.nativePath :
          null;
        if (directory) {
          ta.text += "Répertoire: " + directory + "\n";
        }
        var arguments:Array = invokeEvent.arguments;
        if (arguments.length > 0) {
          ta.text += "Arguments: " + arguments.toString();
        }
      }

      private function showInfo(text:String):void {
        log.text += text + "\n";
      }
    ]]>
  </mx:Script>
  <mx:ApplicationControlBar left="0" right="0" bottom="0" height="80" paddingTop="8" paddingLeft="8"
                            paddingBottom="8" paddingRight="8" horizontalGap="8" verticalGap="8"
                            dock="true">
    <mx:Label id="log" fontSize="18" />
  </mx:ApplicationControlBar>
  <mx:TextArea id="ta" backgroundColor="white" width="100%" height="100%" selectable="false" />
</mx:Application>

Télécharger le projet au format FXP

Télécharger l'application au format APK

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" paddingLeft="0" paddingRight="0"
paddingTop="0" paddingBottom="0"
applicationComplete="application1_applicationCompleteHandler(event)">
<mx:Style>
global {
font-size: 24;
}
</mx:Style>
<mx:Script>
<![CDATA[
import mx.events.FlexEvent;

protected function application1_applicationCompleteHandler(event:FlexEvent):void {
NativeApplication.nativeApplication.addEventListener(InvokeEvent.INVOKE, onInvoke);
}

private function onInvoke(invokeEvent:InvokeEvent):void {
ta.text = "";
var directory:String = invokeEvent.currentDirectory ? invokeEvent.currentDirectory.nativePath :
null;
if (directory) {
ta.text += "Répertoire: " + directory + "\n";
}
var arguments:Array = invokeEvent.arguments;
if (arguments.length > 0) {
ta.text += "Arguments: " + arguments.toString();
}
}

private function showInfo(text:String):void {
log.text += text + "\n";
}
]]>
</mx:Script>
<mx:ApplicationControlBar left="0" right="0" bottom="0" height="80" paddingTop="8" paddingLeft="8"
paddingBottom="8" paddingRight="8" horizontalGap="8" verticalGap="8"
dock="true">
<mx:Label id="log" fontSize="18" />
</mx:ApplicationControlBar>
<mx:TextArea id="ta" backgroundColor="white" width="100%" height="100%" selectable="false" />
</mx:Application>

Articles similaires