ITeye博客

android 检查网络是否连接了

80x86 · 2009-11-30 16:26 · Markdown

[下载]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>

评论(4)

  1. #4 楼 · left · 2009-12-05 18:37
  2. #3 楼 · nj_first · 2009-12-04 13:05

    楼主有研究过怎样改变当前正在使用的APN吗? 通过程序切换!

  3. #2 楼 · sunburst · 2009-12-03 13:58

    差点一不小心点击了。。。。

  4. #1 楼 · lordhong · 2009-12-01 03:57

    上面一个链接算是软性广告吗?

评论功能维护中,当前仅展示历史评论。