利用广播机制判断当前网络状态

来源:互联网 发布:淘宝上买电影票能用吗 编辑:程序博客网 时间:2024/06/05 00:53

日常的应用中,我们常常需要监听当前的网络状态,这个时候我们常常用到的是广播机智

 intentFilter = new IntentFilter();        intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");        netWorkChangeReceiver = new NetWorkChangeReceiver();        registerReceiver(netWorkChangeReceiver, intentFilter);

网络状态改变的监听

class NetWorkChangeReceiver extends BroadcastReceiver {        @Override        public void onReceive(Context context, Intent intent) {            ConnectivityManager connectionManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);            NetworkInfo netWorkInfo = connectionManager.getActiveNetworkInfo();            if (netWorkInfo != null && netWorkInfo.isAvailable()) {                if (netWorkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {                    Toast.makeText(context, "当前网络是3G", Toast.LENGTH_LONG).show();                } else if (netWorkInfo.getType() == ConnectivityManager.TYPE_WIFI) {                    Toast.makeText(context, "当前网络是Wifi", Toast.LENGTH_SHORT).show();                }            } else {                Toast.makeText(context, "网络不可用", Toast.LENGTH_SHORT).show();            }        }    }

在程序销毁的时候要将广播一块销毁

protected void onDestroy() {        super.onDestroy();        unregisterReceiver(netWorkChangeReceiver);    }

还有一点不能忘记,我们在判断当前网络状态是否改变的时候,需要获取网络监听的权限,这一点很重要,也很容易忽视。如果不添加获取网络的权限的话,程序在运行的过程中会直接崩溃。

当然我们也可以将这些封成工具类,然后直接调用

/** * Created by cxz on 2016/5/31 0031. */public class NetworkProber {/**     * 网络是否可用*     * @param context* @return*/public static boolean isNetworkAvailable(Context context) {        ConnectivityManager connectivity = (ConnectivityManager) context                .getSystemService(Context.CONNECTIVITY_SERVICE);        if (connectivity == null) {        } else {            NetworkInfo[] info = connectivity.getAllNetworkInfo();            if (info != null) {for (int i = 0; i < info.length; i++) {if (info[i].getState() == NetworkInfo.State.CONNECTED) {return true;}                }            }        }return false;}/**     * Gps是否打开*     * @param context* @return*/public static boolean isGpsEnabled(Context context) {        LocationManager locationManager = ((LocationManager) context                .getSystemService(Context.LOCATION_SERVICE));List<String> accessibleProviders = locationManager.getProviders(true);        return accessibleProviders != null && accessibleProviders.size() > 0;}/**     * wifi是否打开*/public static boolean isWifiEnabled(Context context) {        ConnectivityManager mgrConn = (ConnectivityManager) context                .getSystemService(Context.CONNECTIVITY_SERVICE);TelephonyManager mgrTel = (TelephonyManager) context                .getSystemService(Context.TELEPHONY_SERVICE);        return ((mgrConn.getActiveNetworkInfo() != null && mgrConn                .getActiveNetworkInfo().getState() == NetworkInfo.State.CONNECTED) || mgrTel                .getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS);}/**     * 判断当前网络是否是wifi网络* if(activeNetInfo.getType()==ConnectivityManager.TYPE_MOBILE) { //判断3G网*     * @param context* @return boolean     */public static boolean isWifi(Context context) {        ConnectivityManager connectivityManager = (ConnectivityManager) context                .getSystemService(Context.CONNECTIVITY_SERVICE);NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();        if (activeNetInfo != null&& activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {return true;}return false;}/**     * 判断当前网络是否是3G网络*     * @param context* @return boolean     */public static boolean is3G(Context context) {        ConnectivityManager connectivityManager = (ConnectivityManager) context                .getSystemService(Context.CONNECTIVITY_SERVICE);NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();        if (activeNetInfo != null&& activeNetInfo.getType() == ConnectivityManager.TYPE_MOBILE) {return true;}return false;}//另外还有两个方法判断网络是否可用:public static boolean isNetworkAvailable_00(Context context) {        ConnectivityManager cm = ((ConnectivityManager) context                .getSystemService(Context.CONNECTIVITY_SERVICE));        if (cm != null) {            NetworkInfo info = cm.getActiveNetworkInfo();            if (info != null && info.isConnectedOrConnecting()) {return true;}        }return false;}public static boolean isNetworkAvailable_01(Context context) {        ConnectivityManager cm = (ConnectivityManager) context                .getSystemService(Context.CONNECTIVITY_SERVICE);NetworkInfo network = cm.getActiveNetworkInfo();        if (network != null) {return network.isAvailable();}return false;}// 更加严谨的写法:public static boolean checkNet(Context context) {try {            ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);            if (connectivity != null) {                NetworkInfo info = connectivity.getActiveNetworkInfo();                if (info != null && info.isConnected()) {if (info.getState() == NetworkInfo.State.CONNECTED) {return true;}                }            }        } catch (Exception e) {return false;}return false;}}
0 0
原创粉丝点击