Friday, September 11, 2009

CHECKING NETWORK & INTERNET LIVE OR NOT in AIR


NETWORK EVENT:

Using the NETWORK_CHANGE event, Add this listener in the applications - creationComplete property.

NativeApplication.nativeApplication.addEventListener(Event.NETWORK_CHANGE, onNetworkChangeHandler);

private function onNetworkChangeHandler(event:Event):void
{
Alert.show("Network Event Triggered");
}

Check the above scenario by disabling & enabling the Network connections manually in start->settings->Network Connections.


IS INTERNET LIVE OR NOT:

Using the URLMonitor class, The defined URL is monitored , If the network is failed , The event dispatches the status using the urlMonitor.available property.Refer the below code:

private var urlMonitor:URLMonitor;

private function checkNetworkLive():void
{
var url:URLRequest
= new URLRequest("http://www.google.com/");
url.method = "HEAD";
urlMonitor = new URLMonitor(url);
urlMonitor.pollInterval = 3000;
urlMonitor.addEventListener(StatusEvent.STATUS,statusChanged);
urlMonitor.start();
}

public function statusChanged(event:StatusEvent):void
{
if(urlMonitor.available)
{
Alert.show("InterNet Live");
}
else
{
Alert.show("InterNet Failed");
}
}

PDF FILE LOADER IN AIR


You can add a PDF to an AIR application by creating an HTMLLoader instance, setting its dimensions, and loading the path of a PDF.The following example loads a PDF from an external site. Replace the URLRequest with the path to an available external PDF.

import mx.core.Application;

public function loadPdfUI(path:String):void
{
if(hasAdobeReader())
{
var htmlloader:HTMLLoader = HTMLLoader.createRootWindow(true);
htmlloader.window.document.title="PDF CONTAINER - "+path;
htmlloader.window.document.width = Application.application.width;
htmlloader.window.document.height = Application.application.height;
var url:URLRequest=new URLRequest(decodeURIComponent(path));
htmlloader.x = 0; htmlloader.y = 0;
htmlloader.load(url);
}
}

private function hasAdobeReader():Boolean
{
return (HTMLLoader.pdfCapability == HTMLPDFCapability.STATUS_OK) ? true : false;
}

The hasAdobeReader() method is used to detect the Adobe Reader is in the system.