-2

found a code that will check internet connectivity here

but I do not have any idea how to implement or call this class or method as I am still studying android programming using android studio.

Please see my code below and kindly let me know how to arrange it in a way that it will fire on application launch plus including the toast message stating that it is connected or not..

package com.example.enan.checkinternetconnection;


public class MainActivity extends AppCompatActivity {

private static final String TAG = "";
private static final String LOG_TAG = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
}

public boolean hasActiveInternetConnection(Context context) {
    if (isNetworkAvailable(context)) {
        try {
            HttpURLConnection urlc = (HttpURLConnection) (new URL("http://www.google.com").openConnection());
            urlc.setRequestProperty("User-Agent", "Test");
            urlc.setRequestProperty("Connection", "close");
            urlc.setConnectTimeout(1500);
            urlc.connect();
            return (urlc.getResponseCode() == 200);
        } catch (IOException e) {
            Log.e(LOG_TAG, "Error checking internet connection", e);
        }
    } else {
        Log.d(LOG_TAG, "No network available!");
    }
    return false;
}

public boolean isNetworkAvailable(Context context) {
    ConnectivityManager connectivityManager
            = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null;
}
}
Community
  • 1
  • 1
fernan
  • 25
  • 1
  • 7

3 Answers3

3

First of all, calling a web page and waiting for its response is NOT a good option when trying to determine whether there is or is not an available internet connection.

There are android built-in helper methods to check for connectivity such as:

private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager 
          = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

Also, if what you want is to check the connectivity on application's launch, the best option is to create a new class that extends from android.app.Application and override the onCreate method as follows:

public class YourApplication extends android.app.Application {
    @Override
    public void onCreate() {
        super.onCreate();
        if (isNetworkAvailable()) {
            //Connected to the Internet
        } else {
            //Not connected
        }
    }
}

Full code would look like this:

public class YourApplication extends android.app.Application {
    @Override
    public void onCreate() {
        super.onCreate();
        if (isNetworkAvailable()) {
            //Connected to the Internet
        } else {
            //Not connected
        }
    }

    private boolean isNetworkAvailable() {
        ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    }
}

Finally, on your AndroidManifest.xml set the name as android:name=".YourApplication"

<application
    android:name=".YourApplication"
    ... >

</application>
Diego Marcher
  • 378
  • 3
  • 7
3

first add this permision to your manifest:

 <uses-permission android:name="android.permission.INTERNET" />

Then in your main Activity innitiaize the connectivity manager:

 private boolean connected(){
  ConnectivityManager connectivityManager=(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
  NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
  return activeNetworkInfo !=null && activeNetworkInfo.isConnected();
}

And in your onCreate(Main Activities oncreate)check if user is connected.For the UI message,you can always add your own custom snackbar or dialog.

if(connected()){
        Log.i("TRUE","User is connected");
    }else{
        Log.i("TRUE","User is not connected");
    }
RileyManda
  • 2,536
  • 25
  • 30
0

Call hasActiveInternetConnection(getApplicationContext()); inside onCreate method.

Aston
  • 1
  • 2
  • I got this error msg upon trying your sugestion "Unfortunately CheckInternetConnection has stoped" – fernan Apr 23 '16 at 15:47
  • Can you post the logcat output? It might because of the missing "ACCESS_NETWORK_STATE" and "INTERNET" permissions. Refer to this website http://developer.android.com/training/basics/network-ops/connecting.html – Aston Apr 23 '16 at 16:00
  • You might as well remove the parameters from both function as the parameter is never been used. – Aston Apr 23 '16 at 16:12