0

I am trying to pull as well as push some data to and from the server via webservice. The mandatory thing that should i have to do is connectivity check. What i have done right now is , i have written a connectivity check code in each activity before it pushes/ pulls the result set from the server. I know its not a best way that i should have to code. Instead this connectivity check should be running some thing like a background , (behind the screens) and alerts the user, when the WIFI / 3G becomes low / goes down.

What is the best way to do so ?

Please let me know know your thoughts.

Thank you.

Nandagopal T
  • 2,037
  • 9
  • 42
  • 54

4 Answers4

2

Hi i do these way maybe there better

private boolean checkInternetConnection() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

    // test for connection
    if (cm.getActiveNetworkInfo() != null
            && cm.getActiveNetworkInfo().isAvailable()
            && cm.getActiveNetworkInfo().isConnected()) {
        return true;
    } else {
        //no conection
        return false;
    }
}
PedroAGSantos
  • 2,336
  • 2
  • 17
  • 34
  • and i think these is already an answered question try to search fisrt – PedroAGSantos May 23 '11 at 07:32
  • Yeah, Thanks a lot. but i want the same to be run as a service or like a broadcastreceiver that runs through out the application from launch to end and checks for the connectivity and intimates me , when it gets down. if you have any ideas on this ,please share... – Nandagopal T May 24 '11 at 09:01
2

You can register a BroadcastReceiver to listen for connectivity changes. A detailed post can be found here.

Community
  • 1
  • 1
Gabriel Negut
  • 13,860
  • 4
  • 38
  • 45
  • Thanks Gabriel, I went through implementation of that code.But when i tried to show alertdialog instead of toast it shows me an error. I know this should come only within an activity, can you suggest me how to do so ? This alertDialog will contain two buttons like retry and cancel. Once i give retry it will check for the connectivity again or cancel to cancel the dialog. – Nandagopal T May 24 '11 at 08:56
  • Considering this is a completely different issue, I think it would be best to ask a new question. – Gabriel Negut May 24 '11 at 17:03
1
public static boolean isInternetAvailable(Context context){
        ConnectivityManager connec = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        android.net.NetworkInfo wifi = connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        android.net.NetworkInfo mobile = connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        if(wifi.isConnected() || mobile.isConnected()){
            // Check for web site       
            try{
                // Create a URL for the desired page
                URL url = new URL("http://www.google.com");
                // Read all the text returned by the server
                BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
                in.close();
                return true;
            } catch (Exception e) {
                return false;
            }
        }

        return false;
    }   

The method also checks whether a certain website in this case the www.google.com is available. This might be useful as the device might be connected to a WLAN router which has no internet access. In this case wifi.isConnected() would also return true although no internet is available.

Flo
  • 27,355
  • 15
  • 87
  • 125
  • Yeah, Thanks a lot. but i want the same to be run as a service or like a broadcastreceiver that runs through out the application from launch to end and checks for the connectivity and intimates me , when it gets down. if you have any ideas on this ,please share... – Nandagopal T May 24 '11 at 09:01
0

for check internet connection in android..

public static boolean isOnline(Activity act) 
     {
         ConnectivityManager cm = (ConnectivityManager)act.getSystemService(Context.CONNECTIVITY_SERVICE);
         NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnectedOrConnecting()) 
        {
            return true;
        }
        return false;

     }
Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
  • Yeah, Thanks a lot. but i want the same to be run as a service or like a broadcastreceiver that runs through out the application from launch to end and checks for the connectivity and intimates me , when it gets down. if you have any ideas on this ,please share... – Nandagopal T May 24 '11 at 09:00