标签:new splay access cloc class androi 网络连接 fine sys
目录结构:
获取网络信息需要在AndroidManifest.xml文件中加入相应的权限。
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
public boolean isNetworkConnected(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo(); if (mNetworkInfo != null) { return mNetworkInfo.isAvailable(); } } return false; }
public boolean isWifiConnected(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mWiFiNetworkInfo = mConnectivityManager .getNetworkInfo(ConnectivityManager.TYPE_WIFI); if (mWiFiNetworkInfo != null) { return mWiFiNetworkInfo.isAvailable(); } } return false; }
这里需要注意,该方法只是判断当前环境下是否有可用的WIFI,并不是判断是否已经连接上WIFI。
public boolean isMobileConnected(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mMobileNetworkInfo = mConnectivityManager .getNetworkInfo(ConnectivityManager.TYPE_MOBILE); if (mMobileNetworkInfo != null) { return mMobileNetworkInfo.isAvailable(); } } return false; }
public static int getConnectedType(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo(); if (mNetworkInfo != null && mNetworkInfo.isAvailable()) { return mNetworkInfo.getType(); } } return -1; }
getType()的返回值可以是ConnectivityManager中的TYPE_MOBILE、TYPE_WIFI、TYPE_WIMAX、TYPE_ETHERNET、TYPE_BLUETOOTH、其他类型。
定义广播接受者:
BroadcastReceiver connectionReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mNetworkInfo = mConnectivityManager .getActiveNetworkInfo(); int type = -1; if (mNetworkInfo != null && mNetworkInfo.isAvailable()) { type = mNetworkInfo.getType(); } if (type == ConnectivityManager.TYPE_WIFI) { Toast.makeText(MainActivity.this, "wifi", Toast.LENGTH_SHORT) .show(); } else if (type == ConnectivityManager.TYPE_MOBILE) { Toast.makeText(MainActivity.this, "网络可用", Toast.LENGTH_SHORT) .show(); }else if(type==-1){ Toast.makeText(MainActivity.this, "网络不可用", Toast.LENGTH_SHORT) .show(); }else{ Toast.makeText(MainActivity.this, "未知网络", Toast.LENGTH_SHORT) .show(); } } };
在合适的位置注册:
IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); registerReceiver(connectionReceiver, intentFilter);
在合适的位置取消注册:
unregisterReceiver(connectionReceiver);
标签:new splay access cloc class androi 网络连接 fine sys
原文地址:https://www.cnblogs.com/HDK2016/p/8978154.html