1

I want to know how to check internet connection is available or not before the application start. when click the application button in application list i want to show dialog box connect to internet or please connect to internet before start this application.

I tried this but i couldn't.

Can anyone help me?

anuruddhika
  • 1,549
  • 8
  • 26
  • 40

5 Answers5

2

Try this:

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

You should call this from your first Activity's onCreate, or if you want to really go the whole way, you could override Application, and call it there (you'll need to wire it up in your activity).

Hope this helps.

Gil Moshayof
  • 16,633
  • 4
  • 47
  • 58
  • That looks good. I'd add a check that the network is connected, using NetworkInfo.isConnected(). See example on http://android.codota.com/scenarios/51891727da0a4288a823cbdc/android.net.ConnectivityManager?tag=out_2013_05_05_07_19_34 – drorw Oct 08 '13 at 06:59
1

Step 1: Add the permission in Manifest.xml to access the NetworkState.

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

Step 2:

public static boolean isNetworkAvailable(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnected()) {
        return true;
    }
    return false;
}

Try these steps, If you have any issue let me know...:)

Vishesh Chandra
  • 6,951
  • 6
  • 35
  • 38
  • i want to identify it with the application load. so to where should i add this? – anuruddhika Oct 08 '13 at 06:55
  • You can keep this method in Utility.java and you can call this method from **onCreate() of Application class**. For more detail about Application class follow [this link](http://developer.android.com/reference/android/app/Application.html). – Vishesh Chandra Oct 08 '13 at 07:17
0
public boolean isNetworkAvailable(Context ct)
{
    ConnectivityManager connectivityManager = (ConnectivityManager) ct.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null;
}

Just send context here. If internet is there it will return True else False

Sumant
  • 2,775
  • 2
  • 22
  • 30
0

Try this:

public static boolean isOnline(Context ctx) {

    NetworkInfo info = (NetworkInfo) ((ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
    if (info == null && !info.isConnected() ) {
        return false;
    }
    if (info.isRoaming()) {
        return false;
    }
    return true;
}
Sardor Dushamov
  • 1,665
  • 3
  • 17
  • 44
0

Use following code.

package com.example.app;

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


public class ConnectionManager {

    private Context context;

    public ConnectionManager(Context context){
        this.context = context;
    }
    public boolean isConnectingToInternet(){

        ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo[] info = cm.getAllNetworkInfo();
        if (info != null){
            for (int i = 0; i < info.length; i++){
                if (info[i].getState() == NetworkInfo.State.CONNECTED){
                    return true;
                }
            }
        }
        return false;
    }
}

Then call isConnectingToInternet in your MainActivity like below.

ConnectionManager cm = new ConnectionManager(this);

boolean connection = cm.isConnectingToInternet();
Jitesh Dalsaniya
  • 1,917
  • 3
  • 20
  • 36
  • Hi, once I have connected to my Wi-Fi this method returns true but there is no internet connection available so how do I detect if the network is really available – Maulik Sheth Oct 08 '13 at 06:42
  • Then you have to call isConnectingToInternet() method in background that continuously checking for availability for internet connection. – Jitesh Dalsaniya Oct 08 '13 at 07:07