How to know whether there is any network connection switch or network connection lost in my application.
            Asked
            
        
        
            Active
            
        
            Viewed 1,475 times
        
    0
            
            
        - 
                    what protocol you are using? in Socket it will be easy to know that connection is lost, in Http there will be different way – Mohammad Ersan Sep 08 '11 at 08:16
 
2 Answers
0
            
            
        you can use the ConnectivityManager in Android:
ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo net = cm.getActiveNetworkInfo();
if(net != null && net.isConnected()) {
    // we have connection
}
Edit: Checking for internet connection requires some permissions as well:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
        NewProggie
        
- 1,015
 - 1
 - 10
 - 24
 
0
            
            
        You can refer my post over here for network switch and checking connection: (Note: I have posted answer below question in that post)
How to handle WiFi to Mobile network switch programatically?
You have to add some permissions in AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
        Community
        
- 1
 - 1
 
        Balaji Khadake
        
- 3,449
 - 4
 - 24
 - 38