Using AIR 1.5.1 InovkeReason for friendlier applications

I recently discovered a new feature of AIR 1.5.1 that makes AIR applications more consumer friendly in certain situations. When you have your application set to start on user login using “NativeApplication.nativeApplication.startAtLogin = true”, you may want the application to start as a background process rather than show the entire application at login. In AIR 1.5.1 there is a new property of the InvokeEvent called “reason” that will report if the application was started at login or was started when the users invokes the application by launching the application.

By capturing this event and testing for InovokeReason.LOGIN, you can keep your application invisible and only show the windows docked in the system tray. If a users starts the application it will be InvokeReason.STANDARD and you can then show the application as normal. I find it annoying to have a ton of windows popping up on my screen when I restart my computer.

I usually set up an InvokeEvent when a users clicks on the system tray icon to activate the application and bring it to the forefront. I initially set WindowsApplicatoin visible=”false” so the application is invisible on start. If the the InvokeEventReason is “LOGIN”, I keep the application invisible, in other cases I activate the application and bring it to the forefront.

It should be noted that the InvokeEvent fires on application start up as well as from running the the application from the IDE (so you can’t test it fully unless you install the application and login again). Also, because of timing of events at start up, the application is invisible even when you set it to active, so it will blink if the InvokeEventReason is STANDARD. To avoid this behavior, I use the “windowComplete” event of the WindowedApplication to add the InvokeEvent listeners. So the code might look something like the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"  windowComplete="onAppInit()"  visible="false"  >

<mx:Script>
        <![CDATA[

            private function onAppInit():void
            {
                if (NativeApplication.supportsSystemTrayIcon) { // windows
                    setSystemTrayProperties();
                } else if(NativeApplication.supportsDockIcon) { // mac
                    setDockProperties();
                }
            }

                       // add click event to the system tray
                       private function setSystemTrayProperties():void
            {
                var sysTray:SystemTrayIcon = NativeApplication.nativeApplication.icon as SystemTrayIcon;
                sysTray.addEventListener(MouseEvent.CLICK, onIconClick, false, 0, true);
                               
                               NativeApplication.nativeApplication.addEventListener(InvokeEvent.INVOKE, onIconClick, false, 0, true);
            }
           
                       // add click event to the system tray
            private function setDockProperties():void
            {
                NativeApplication.nativeApplication.addEventListener(InvokeEvent.INVOKE, onIconClick, false, 0, true);
            }

                       private function onIconClick(event:InvokeEvent):void
            {
                trace("INVOKE REASON: " + event.reason);
                if(event.reason != InvokeEventReason.LOGIN) {
                                   if (NativeApplication.supportsSystemTrayIcon) {
                                      stage.nativeWindow.visible = true;  // win
                                   } else {
                                      stage.nativeWindow.activate();  // mac
                                   }
                    stage.nativeWindow.orderToFront();
                               {
                       }
    ]]>
</mx:Script>

- Mister

  • Facebook
  • Twitter
  • Google Bookmarks
  • RSS
This entry was posted in AIR and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

6 Comments

  1. Martyn
    Posted October 14, 2009 at 5:48 am | Permalink

    Hi,

    Thanks for your interesting article.

    I’m trying to incorporate the same thing in my code but my application always has an InvokeEvent.reason of “standard”, including on login in Windows XP.

    Have you ever had any such issues?

    Thanks,

    Martyn

  2. Posted October 18, 2009 at 8:44 am | Permalink

    Strange, do you have the AIR 1.5.2 SDK installed in your Eclipse? Alse be sure to try outside the IDE.

  3. Martyn
    Posted October 19, 2009 at 2:36 am | Permalink

    Yes, I’m using 1.5.2 – I followed this article from Adobe – http://www.adobe.com/devnet/air/ajax/quickstart/startup_options.html – and after installing the sample app and restarting, the value for InvokeEvent.reason was ‘standard’, which is really confusing

  4. Posted October 20, 2009 at 9:48 am | Permalink

    You might want to post the issue to AIR-tight Google group and see what some other say.

  5. Shaning
    Posted January 8, 2010 at 11:25 am | Permalink

    Dear Mister:
    I was impressed on the demo for drag & drop to eliminate duplicates at:
    http://thanksmister.com/?p=21
    However, there is only demo at http://thanksmister.com/dnddatagrid/index.html rather than the source as you stated. I would appreciate if you can provide the source code for it.
    Thanks!

  6. Posted January 18, 2010 at 4:22 pm | Permalink

    Are you aware that there is a feature of Flex applications that you can actually right-click to view source? You should be familiar with this option as its used in many code examples.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use [as]...[/as] to post ActionScript code in your comments. Example code in comment: [as] public var myvar:String = "Hello"; [/as]