Android详解Android 网络操作
Posted hdk2016
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android详解Android 网络操作相关的知识,希望对你有一定的参考价值。
目录结构:
contents structure [+]
获取网络信息需要在androidManifest.xml文件中加入相应的权限。
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
1.判断网络
1.1 判断是否有网络连接
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; }
1.2判断WIFI网络是否可用
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。
1.3 判断MOBILE网络是否可用
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; }
1.4 获取当前网络连接的类型信息
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、其他类型。
2.监听网络
定义广播接受者:
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);
以上是关于Android详解Android 网络操作的主要内容,如果未能解决你的问题,请参考以下文章
Android UriUriMatcherContentUris详解