0

Let's just say I have an simple hello world app..now I want my app to open Only when there is Internet connectivity otherwise it should display no internet connection available message..thanks in advance

  • Take a look at this post http://stackoverflow.com/questions/4238921/detect-whether-there-is-an-internet-connection-available-on-android . There are many different solutions there. – UserName_Untold Mar 03 '16 at 06:43

1 Answers1

0

Use this method in your first activity,

/**
 * @return true if network is available and presents no UI message.
 */
public void isNetworkAvailable() {
    try {
        ConnectivityManager connectivity = (ConnectivityManager) sContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null)
                for (int i = 0; i < info.length; i++)
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        //present your UI
                    }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    //exit the app
    finish();
}

Do add the android.permission.ACCESS_NETWORK_STATE permission in Manifest.

Shubham A.
  • 2,446
  • 4
  • 36
  • 68