Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Adobe AIR Development

Reply
Developer
Posts: 58
Registered: ‎02-13-2009
My Device: Not Specified

Checking for intermittent drop in internet connectivity

In my application, I want to detect change in internet connectivity. For that I am using 

 

flash.desktop.NativeApplication;NativeApplication.nativeApplication.addEventListener(Event.NETWORK_CHANGE, onNetworkChange);

 

In the onNetworkChange() function, I am doing this

 

 

monitor = new URLMonitor(new URLRequest('http://www.google.com'));
 monitor.addEventListener(StatusEvent.STATUS, checkNetworkStatus);
 monitor.start();

 

 

In the checkNetworkStatus() function, I am now checking for the "monitor.available" which returns boolean true or false indicating the status of the internet connectivity. But looks like the onNetworkChange() is never reached. I am simulating the network down by disconnecting the network adapter in the VMware player for the simulator.

 

I have separately enabled the event listener below at the start of the application which works fine so that I can take appropriate actions.

 

 

monitor = new URLMonitor(new URLRequest('http://www.google.com'));
 monitor.addEventListener(StatusEvent.STATUS, checkNetworkStatus);
 monitor.start();

 

 

Any suggestions to detect the drop in internet connectivity while in the application?

Developer
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: Checking for intermittent drop in internet connectivity

hey,

 

according to what i've read about Event.NETWORK_CHANGE it is sometimes unreliable. i wrote up a sample code a while ago dealing with checking your interenet connection. the following code after you press start test will poll and check your interent connection and let you know when you disconnect:

 

 

package
{
	import air.net.ServiceMonitor;
	import air.net.URLMonitor;
	
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.MouseEvent;
	import flash.events.StatusEvent;
	import flash.net.URLRequest;
	import flash.text.TextFieldAutoSize;
	
	import qnx.ui.buttons.LabelButton;
	import qnx.ui.text.Label;
	
	public class NetworkTest extends Sprite
	{
		private var myBtn:LabelButton;
		private var myLabel:Label;
		
		public function NetworkTest()
		{
			super();
			
			// support autoOrients
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			myBtn = new LabelButton();
			myBtn.setPosition(10,10);
			myBtn.label = "Start Test";
			myBtn.setSize(150, 50);
			
			myBtn.addEventListener(MouseEvent.CLICK, onClick);
			
			addChild(myBtn);
			
			myLabel = new Label();
			myLabel.text = "Status: Not Started";
			myLabel.autoSize = TextFieldAutoSize.LEFT;
			myLabel.setPosition(10,60);
			
			addChild(myLabel);
		}
		
		private function onClick(e:MouseEvent):void
		{
			startTest();
		}
		
		private function startTest():void
		{
			var urlMonitor:URLMonitor;
			var urlRequest:URLRequest = new URLRequest( "http://google.com" );
			urlMonitor = new URLMonitor( urlRequest );
			urlMonitor.addEventListener( StatusEvent.STATUS, onStatusEvent );
			urlMonitor.start();

		}
		
		private function onStatusEvent(e:StatusEvent):void
		{
			myLabel.text =  "Status: " + e.code;

		}
	}
}

 

the event is started by clicking a button but can easily be adapted to start once your application starts using Event.ENTER_FRAME. i am not entirely sure how this will effect your battery life because from my tests it seems to be pretty fast in detecting a dropped connection (via AIR Desktop testing). hope this can point you in the right direction though. good luck!

 

J. Rab (Blog) (Twitter)
--
1. If you liked my post or found it useful please click on the thumbs up and provide a Like!
2. If my post solved your problem please click on the Accept as Solution button. Much appreciated!

Approved Apps: OnTrack | ssShots | Hangman
Developer
Posts: 85
Registered: ‎10-25-2010
My Device: Not Specified

Re: Checking for intermittent drop in internet connectivity

Hi,

JRab is right, Event.NETWORK_CHANGE will only tell you that something happened, not if you are online or not, you will always have to test sending a simple URLRequest (you can only request the HEAD headers, you don't need the response).

In a Desktop environment, Event.NETWORK_CHANGE is fired when you connect to a VPN, and it doesn't mean u lost connectivity

Hope this helps,
Fabien

--
1. If you liked my post or found it useful please click on the white star and provide a Kudo!
2. If my post solved your problem please click on the Accept as Solution button. Much appreciated!