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?
            Asked
            
        
        
            Active
            
        
            Viewed 352 times
        
    0
            
            
         
    
    
        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 Answers
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
- 
                    so, where should I call this and what can I do if connection return false? – jjLin Aug 24 '12 at 03:09
- 
                    that's a whole new question...you call this when you need it, if it returns false you know there's no connection... – slinden77 Aug 24 '12 at 04:28