How to know change in network connection in android
How to know whether there is any netwo开发者_如何转开发rk connection switch or network connection lost in my application.
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" />
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" />
精彩评论