android 检查网络是否连接了

2023-05-16

[下载]php java javascript 相关 api 手册的下载

 代码如下

 

/**

 * 

 * @param activity

 * @return boolean return true if the application can access the internet

 */

public static boolean hasInternet(Activity activity) {

ConnectivityManager manager = (ConnectivityManager) activity

.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo info = manager.getActiveNetworkInfo();

if (info == null || !info.isConnected()) {

return false;

}

if (info.isRoaming()) {

// here is the roaming option you can change it if you want to

// disable internet while roaming, just return false

return true;

}

return true;

 

}

 

需要在AndroidManifest.xml中
增加权限:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

android 检查网络是否连接了 的相关文章

随机推荐