Adding an icon in the System Tray with Adobe AIR

Sometimes you need your application to rum in “background” simply enter it with an icon int the systray of your computer. Adobe Air supports thos functionality, is possible use the interaction both the right and left mouse button.
Let’s take a pecture for all our example, like this: brick
Will carry the image in our application using the Loader class, so the loading is complete, will be adding to the system tray using the property NativeApplication.nativeApplication.icon.bitmaps.
[ACTIONSCRIPT3]
public function init():void
{
var icon:Loader = new Loader();
icon.contentLoaderInfo.addEventListener(Event.COMPLETE, iconLoadComplete);
icon.load(new URLRequest(“brick.png”));
}

private function iconLoadComplete(event:Event):void
{
NativeApplication.nativeApplication.icon.bitmaps = [event.target.content.bitmapData];
}
[/ACTIONSCRIPT3]
Running the application. see the picture next to the windows clock. Let’s add a tooltip:
[ACTIONSCRIPT3]
public function init():void
{
var icon:Loader = new Loader();
icon.contentLoaderInfo.addEventListener(Event.COMPLETE, iconLoadComplete);
icon.load(new URLRequest(“brick.png”));
var systray:SystemTrayIcon = NativeApplication.nativeApplication.icon as SystemTrayIcon;
if (NativeApplication.supportsSystemTrayIcon)
{
systray.tooltip = “to aqui ô!!! o/”;
}
}
[/ACTIONSCRIPT3]
Running the program will se the icon apperting in system tray, when passing the mouse over the tooltip.
Now we implement the action for when the user click right mouse button. We will use NativeMenu and NativeMenuItem, is very similar to that already used in Flash Player. We instantiate the class and add an event to select action:
[ACTIONSCRIPT3]
var iconMenu:NativeMenu = new NativeMenu();
var aboutCommand:NativeMenuItem = iconMenu.addItem(new NativeMenuItem(“blablabla”));
var webCommand:NativeMenuItem = iconMenu.addItem(new NativeMenuItem(“Ir para o blog”));
webCommand.addEventListener(Event.SELECT,goURL);
iconMenu.addItem(new NativeMenuItem(“”,true));//separator
var exitCommand:NativeMenuItem = iconMenu.addItem(new NativeMenuItem(“Exit”)); exitCommand.addEventListener(Event.SELECT, closeApp);
[/ACTIONSCRIPT3]
Now cheking if the operating system support system tray:
[ACTIONSCRIPT3]
if (NativeApplication.supportsSystemTrayIcon)
{
var systray:SystemTrayIcon = NativeApplication.nativeApplication.icon as SystemTrayIcon;
systray.menu = iconMenu;
}
[/ACTIONSCRIPT3]
Complete code:
DOWNLOAD

Reference:
http://help.adobe.com/en_US/AIR/1.5/devappsflex/WS5b3ccc516d4fbf351e63e3d118666ade46-7dcc.html

Recommended Books:


Translations
Português do Brasil

Was this article helpful? feel free to make a donation and help keep the blog in the air
Adobe AIR

Leave a Reply