开发者

How to know change in network connection in android

开发者 https://www.devze.com 2023-04-03 16:44 出处:网络
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:

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" />
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号