android 检查网络是否连接了

[下载]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中
增加权限:

 

你可能感兴趣的:(移动开发,php,java)