Android Apps

Handle network connectivity check before each app event

May 27, 2012 by Sarath DR in Android Apps, Featured with 0 Comments

Sometimes we need to check the network connectivity before each action in an Android phonegap app. This code is very useful when you converting a mobile web app into phonegap app. You have to check the connection before each action otherwise it will generate a network error in the app. If somebody have any other better idea please post it as comment.

I have written the below java code to check the connectivity and then added javascript interface to that native class to access it from the webpage.

 
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import android.webkit.WebView;
 
 
    public class CustomNativeAccess {
 
    	private WebView mAppView;
        private DroidGap mGap;
 
        public CustomNativeAccess(DroidGap gap, WebView view)
        {
            mAppView = view;
            mGap = gap;
        }   
 
        public  String isNetworkAvailable() 
	{
	   	     ConnectivityManager connectivityManager  = (ConnectivityManager) mGap.getSystemService( Context.CONNECTIVITY_SERVICE);
	   	     NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
 
	   	        if( activeNetworkInfo != null )
	   	        {
	   	        	return true;
	   	        }
 
	   	        return false;
	  }
 
    }

Add the below code to your onCreate function to add javascript interface.

 CustomNativeAccess cna = new CustomNativeAccess(this, appView);    
 appView.addJavascriptInterface(cna, "CustomNativeAccess");

You can use the below javascript code to check the connection.

 
        function checkConnection()
        {
           if( window.CustomNativeAccess.isNetworkAvailable() == false )
           {
                showNoConnectionNotification();
                return false;
           }
 
           return true; 
 
        }
 
        function showNoConnectionNotification()
        {
            navigator.notification.confirm(
                "You must have an internet connection to use RingTagz. Please connect and try again",  // message
                 onConnectionLost,              // callback to invoke with index of button pressed
                 "Unable to connect",            // title
                'Try Again, Cancel'       // buttonLabels
            );
        }
 
        function onConnectionLost( button )
        {
            if( button == 1 )
            {
                 checkConnection();
            }
            else
            {
                // navigator.app.exitApp(); // If you want to close the app.
                return false;
            }
 
 
        }

Call function checkConnection() before all the event.

Share this to

Leave a reply

Your email address will not be published. Required fields are marked *

*

About
View Sarath D R's profile on LinkedIn

I am a technology consultant and an entrepreneur focused on the following technologies Java,Spring, Android, Angular, Asterisk, Symfony, Cake , Code-Igniter, Jquery , XAMPP, XHTML, AJAX, MySql, Seo, Web Design, Wordpress, iPhone development.

Today’s Tips
  • Generate Codova.jar file from Phonegap project using ant
  • Google map javascript client  to find the geographic co-ordinates  from  Address or post code
  • Update Android main UI from Webview – Phonegap Plugin
  • Jplayer is not working  with HTC One Android 4.3 version browser
  • Regular expression to validate mobile phone numbers starting with + ( plus ) – php
  • Simple pagination script for PHP
  • Useful adb ( Android Debug Bridge ) commands
  • Change the javascript files url to absolute url – Symfony 1.4
  • Set Http authentication header from Client – PHP / CURL

This site uses cookies from Google to deliver its services, to personalise ads and to analyse traffic. Information about your use of this site is shared with Google. By using this site, you agree to its use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.This site also uses cookies from Google to deliver its services, to personalise ads and to analyse traffic. Information about your use of this site is shared with Google. By using this site, you agree to its use of cookies.

Close