标签:internet dma get context class ati ems for data
有时做Android开发须要用到网络来连接server。假设没有网络则进行提示。代码非常easy,代码例如以下:
public static boolean isNetworkAvailable(Context context) { ConnectivityManager mgr = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo[] info = mgr.getAllNetworkInfo(); if (info != null) { for (int i = 0; i < info.length; i++) { if (info[i].getState() == NetworkInfo.State.CONNECTED) { return true; } } } return false; }用的时候直接引用即可了,如
isNetworkAvailable(MainActivity.this);
不要忘记在AndroidManifest.xml里加入权限:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.INTERNET"/>
标签:internet dma get context class ati ems for data
原文地址:http://www.cnblogs.com/ljbguanli/p/6971347.html