金正昆讲礼仪观后感,大丫鬟演员表,水玉梦醉红楼情
本示例演示如何使用Android API检查网络连接。
Demo下载地址:https://github.com/googlesamples/android-BasicNetworking/#readme
相关API:https://developer.android.google.cn/reference/android/net/ConnectivityManager.html
利用ConnectivityManager来检查是否已经连接网络,如果已经连接,判断网络类型。通过ConnectivityManager.getActiveNetworkInfo()方法获取NetworkInfo对象,可获取网络状态信息。
关键代码:
/** * 检查网络是否已经连接,如果已连接,判断是否WIFI状态或其他网络类型。 */ private void checkNetworkConnection() { ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeInfo = connMgr.getActiveNetworkInfo(); if (activeInfo != null && activeInfo.isConnected()) { wifiConnected = activeInfo.getType() == ConnectivityManager.TYPE_WIFI; mobileConnected = activeInfo.getType() == ConnectivityManager.TYPE_MOBILE; if(wifiConnected) { Log.i(TAG, getString(R.string.wifi_connection)); } else if (mobileConnected){ Log.i(TAG, getString(R.string.mobile_connection)); } } else { Log.i(TAG, getString(R.string.no_wifi_or_mobile)); } }
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Android apk 项目一键打包并上传到蒲公英的实现方法
Android 自定义LineLayout实现满屏任意拖动功能的示例代码
android 限制某个操作每天只能操作指定的次数(示例代码详解)
Android 集成 google 登录并获取性别等隐私信息的实现代码
网友评论