2

i am working on app that must have an internet connection before populating data from the server . I have a bool method that check if the there is a network connection available but it is not working properly.

Here is what is what to Achieve:

1: Check if Wifi Network is Available and there is a network Access

2: Check is the Mobile Data is Enable and there is a Network Access

Problem(Bug) :

Currently, i do not have an internet access but my Mobile data is ON , instead of getting a Toast message that i am not connected to the internet, it will still tell me that i have an internet access but i cannot access the internet.

I have check every other possible means but they all have an approach related to the method i am using.

QUESTION: How can i test if there is an Active Internet Access even if the Mobile Data is Enable .

MY CODE:

private boolean haveConnected() {
        //Vars
        boolean wifiIsConnected = false;
        boolean mobileDataIsConnected = false;
        State checkMobile;
        State checkWifi;
        ConnectivityManager connectManager;

    //init  
        connectManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        checkMobile = connectManager.getNetworkInfo(0).getState();
        checkWifi = connectManager.getNetworkInfo(1).getState();

        if (checkMobile == NetworkInfo.State.CONNECTED) {
            mobileDataIsConnected = true;
        } else if (checkWifi == NetworkInfo.State.CONNECTED) {
            wifiIsConnected = true;

        }
        return wifiIsConnected || mobileDataIsConnected;
    }
hatboy
  • 66
  • 1
  • 8

3 Answers3

0

You should use a BroadcastReciever and let the OS tell you when it connects/disconnects. Then, you can have a class that will keep track of those events. Here is the official description. In addition, there is a StackOverflow question that has several examples that might be helpful to you.

Community
  • 1
  • 1
C0D3LIC1OU5
  • 8,600
  • 2
  • 37
  • 47
  • i have tried getting the network state from a custom receiver but the same issues, it will show connected even when i do not have an active internet access.. – hatboy Jul 21 '15 at 15:06
  • It works if implemented properly. Another helpful link is this: http://stackoverflow.com/questions/15546712/unfortunately-app-is-getting-stopped-while-checking-for-network/15546897#15546897 – C0D3LIC1OU5 Jul 21 '15 at 15:10
  • All those questions are answered, if you are having the same problem please check for the solution. If it isn't the same problem, post your code so we can take a look at what you're doing. – C0D3LIC1OU5 Jul 21 '15 at 15:33
  • BUT IT CHECKED ONLY MOBILE DATA? NetworkInfo activeNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); – hatboy Jul 21 '15 at 15:43
  • `NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo(); networkAvailable = activeNetworkInfo == null ? false : activeNetworkInfo.isConnected();` – C0D3LIC1OU5 Jul 21 '15 at 15:48
  • `public class UpdateReceive extends BroadcastReceiver { public static boolean isConnected1; @Override public void onReceive(Context context, Intent intent) { ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE ); NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo(); boolean networkAvailable = activeNetInfo == null ? false : activeNetInfo.isConnected(); isConnected1=networkAvailable; } }` – hatboy Jul 21 '15 at 15:55
  • I'd use `isConnected1 = activeNetInfo == null ? false : activeNetInfo.isConnected(); ` Otherwise, this should work. – C0D3LIC1OU5 Jul 21 '15 at 15:58
  • Looks fine, if it works, please don't forget to accept the answer. – C0D3LIC1OU5 Jul 22 '15 at 14:01
  • keep in mind that your receiver wont get work properly when you first run the app. It would only work when the connectivity changes while your app is already running. – C0D3LIC1OU5 Jul 22 '15 at 18:24
0

Make a simple request to anywhere in the internet, if it reaches the request you have data ,else you get timeout

admiral
  • 153
  • 4
  • 10
  • i guess a simple pin with 2-3 sec is also ok , try something like this http://stackoverflow.com/questions/3905358/how-to-ping-external-ip-from-java-android – admiral Jul 21 '15 at 17:19
0

You can just check for active networks:

    ConnectivityManager conManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = conManager.getActiveNetworkInfo();

    networkAvailable = activeNetworkInfo != null;

    if (!networkAvailable) { 
        //do your logic
    }
Kristy Welsh
  • 7,828
  • 12
  • 64
  • 106
  • This is simpler solution which can work, too. I would change one line though: `networkAvailable = activeNetworkInfo == null ? false : activeNetworkingInfo.isConnected();` as per the documentation (http://developer.android.com/reference/android/net/ConnectivityManager.html#getActiveNetworkInfo()) – C0D3LIC1OU5 Jul 21 '15 at 15:30