0

In my application, most of the processing in all Activity need to connect to Server to get data.
So, where should I check the Internet connection?
Is there any methods that can tell me when the connection is unavailable?
Finally, how to implement it? Is there any API for handle this case?

jjLin
  • 3,281
  • 8
  • 32
  • 55
  • duplicates http://stackoverflow.com/questions/4238921/android-detect-whether-there-is-an-internet-connection-available http://stackoverflow.com/questions/1560788/how-to-check-internet-access-on-android-inetaddress-never-timeouts http://stackoverflow.com/questions/4086159/checking-internet-connection-on-android – J.K Aug 24 '12 at 02:38
  • For the check the internet connection Go through this http://stackoverflow.com/a/12038094/1263679 – Rahul Patel Aug 24 '12 at 04:06

2 Answers2

0

use the ConnectivityManager class and Context.getSystemService(Context.CONNECTIVITY_SERVICE) method. More information: http://developer.android.com/reference/android/net/ConnectivityManager.html

Ascension
  • 2,599
  • 2
  • 15
  • 13
0
// just checks if network connectivity is available 
public boolean isNetworkConnected(Context context){
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isAvailable() && cm.getActiveNetworkInfo().isConnected()) return true;
    return false;           
}//end method
slinden77
  • 3,378
  • 2
  • 37
  • 35