-4

I need code for this Algorithm in android Eclipse programming :

If internet connection = connect then open program 
else show error_activity.xml
user3751479
  • 9
  • 1
  • 3
  • [http://stackoverflow.com/questions/4086159/checking-internet-connection-on-android?rq=1](http://stackoverflow.com/questions/4086159/checking-internet-connection-on-android?rq=1) – M D Jun 18 '14 at 09:52
  • 1
    Please perform even a minimal amount of research before asking a question. Also, StackOverflow is not for requesting people to write code for you; it is for helping *you* write code. – indivisible Jun 18 '14 at 09:55
  • 2
    to all downvoter,i request to remove,he is a newbie. – Pankaj Arora Jun 18 '14 at 10:02

3 Answers3

1

Create a class ConnectionDetector and put the below code in that class:

private Context _context;

public ConnectionDetector(Context context) {
    this._context = context;
}

public boolean isConnectingToInternet() {
    ConnectivityManager connectivity = (ConnectivityManager) _context
            .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) {
                    return true;
                }

    }
    return false;
}

// now where ever you want to check internet just make a object of this class and call its method like below i am doing:

// in your activity define globals:

Boolean _isInternetPresent = false;
ConnectionDetector _cd;

//in oncreate

    _cd = new ConnectionDetector(getApplicationContext());
    _isInternetPresent = _cd.isConnectingToInternet();

and check like this:

         if (_isInternetPresent) {
            // do you work here
            } else {
                    // no internet,please try again.

           }

as you are a fresher i am giving you complete code.please try to understand each and every step,how this code is working.

Pankaj Arora
  • 10,224
  • 2
  • 37
  • 59
0

try this to check for internet connection..

public void checkNetwork() {
        ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();

        if (networkInfo != null && networkInfo.isConnected()) {
            //true : your code
        } else {
            //false : your code
            Log.v("Network Error", "No network connection available.");
        }
    }
Pragnesh Ghoda シ
  • 8,318
  • 3
  • 25
  • 40
0

Step 1. Create ConnectionDetector.java

package com.YOURPACKAGE.YOURAPP;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class ConnectionDetector {

private Context _context;

public ConnectionDetector(Context context) {
    this._context = context;
}

public boolean checkInternetConn() {
    ConnectivityManager connectivity = (ConnectivityManager) _context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivity != null) {
        NetworkInfo info = connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        if(info == null){
            connectivity.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        }

        if(info == null){
            connectivity.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
        }

        if (info != null) {
            if (info.isConnected()) {
                return true;
            }
        }
    }
    return false;
}
}

Step 2. Implement the Connection Detector in your Activity

public class YOURACTIVITY extends Activity {

    ConnectionDetector cd;

    private void methodThatNeedsTheInternet() {

        Boolean isConnectionExist = false;
        isConnectionExist = cd.checkInternetConn();

        // check for Internet status before proceeding
        if (!isConnectionExist) {
            //no internet, show alert or something
            return;
        }

        //internet works, perform method that needs it
    }

}
Chris Klingler
  • 5,258
  • 2
  • 37
  • 43