zoukankan      html  css  js  c++  java
  • android network develop(2)----network status check

    Check & Get network status

    Normally, there will be two type with phone network: wifi & mobile(gprs,3g,4fg)

    So, we have can test connect and get the connect type.

    1.check connect:

        public static boolean isOnline(Context context)
        {
            ConnectivityManager connMgr = (ConnectivityManager) 
                    context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
            return (networkInfo != null && networkInfo.isConnected());
        }

    2.get connect type:

        public static NetWorkStatus traceConnectStatus(Context context) {
            
            NetWorkStatus mStatus = NetWorkStatus.INVALID;
            ConnectivityManager connMgr = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connMgr
                    .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            boolean isWifiConn = networkInfo.isConnected();
            networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            boolean isMobileConn = networkInfo.isConnected();
            
            if (isMobileConn) {
                if (isWifiConn) {
                    mStatus = NetWorkStatus.BOTH;
                } else {
                    mStatus = NetWorkStatus.GPRS;
                }
            } else {
                if (isWifiConn) {
                    mStatus = NetWorkStatus.WIFI;
                } else {
                    mStatus = NetWorkStatus.INVALID;
                }
            }
            return mStatus;
        }

    3.connect status changed:

    there is an intent we can listener. "android.net.conn.CONNECTIVITY_CHANGE"

    package com.joyfulmath.androidstudy.connect;
    
    import java.lang.ref.WeakReference;
    
    import com.joyfulmath.androidstudy.TraceLog;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.net.ConnectivityManager;
    import android.net.NetworkInfo;
    
    public class NetWorkUtils {
        
        public static final String CONNECTIVITY_CHANGE_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
        public enum NetWorkStatus{
            INVALID,GPRS,WIFI,BOTH
        }
        private WeakReference<Context> mWeakContext = null;
        private ConnectReceiver mConReceiver = null;
    //    private NetWorkStatus mNetWorkStatus = NetWorkStatus.INVALID;
        
        public static NetWorkStatus traceConnectStatus(Context context) {
            
            NetWorkStatus mStatus = NetWorkStatus.INVALID;
            ConnectivityManager connMgr = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connMgr
                    .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            boolean isWifiConn = networkInfo.isConnected();
            networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            boolean isMobileConn = networkInfo.isConnected();
            
            if (isMobileConn) {
                if (isWifiConn) {
                    mStatus = NetWorkStatus.BOTH;
                } else {
                    mStatus = NetWorkStatus.GPRS;
                }
            } else {
                if (isWifiConn) {
                    mStatus = NetWorkStatus.WIFI;
                } else {
                    mStatus = NetWorkStatus.INVALID;
                }
            }
            return mStatus;
        }
        
        public static boolean isOnline(Context context)
        {
            ConnectivityManager connMgr = (ConnectivityManager) 
                    context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
            return (networkInfo != null && networkInfo.isConnected());
        }
        
        
        public NetWorkUtils(Context context)
        {
            mWeakContext = new WeakReference<Context>(context);
        }
        
    
        public void registerConnectReceiver() {
            if (mWeakContext.get() != null) {
                
                if (mConReceiver == null) {
                    mConReceiver = new ConnectReceiver();
                }
                IntentFilter filter = new IntentFilter();
                filter.addAction(CONNECTIVITY_CHANGE_ACTION);
                filter.setPriority(1000);
                mWeakContext.get().registerReceiver(mConReceiver, filter);
            }
    
        }
        
        public void unRegisterConnectReceiver()
        {
            if(mWeakContext.get()!=null && mConReceiver!=null)
            {
                mWeakContext.get().unregisterReceiver(mConReceiver);
                mConReceiver = null;
            }
    
        }
        
        private void connectChanged()
        {
            if(mWeakContext.get()!=null)
            {
                NetWorkStatus status = traceConnectStatus(mWeakContext.get());
                TraceLog.i("status:"+status);
            }
        }
        
        public class ConnectReceiver extends BroadcastReceiver{
    
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                if(action!=null && action.equals(CONNECTIVITY_CHANGE_ACTION))
                {
                    connectChanged();
                }
            }
            
        }
    
    }
  • 相关阅读:
    根据人脸关键点实现平面三角剖分和最近邻搜索 ( KNN, K=1 ), opencv3.4.2, C++
    KDTree  C++实现
    python 保留小数
    Clion提示:Single-argument constructors must be marked explicitly to avoid unintentional implicit conversions 解法办法
    二叉搜索树的C++ 实现
    排列组合之组合问题 网易深度学习工程师面试题 C++ 使用10方法
    OS X 安装命令行看图工具 chafa 以及其依赖libtool
    leetcode704 C++ 72ms 二分查找
    Deep Interest Network for Click-Through Rate Prediction
    归并排序
  • 原文地址:https://www.cnblogs.com/deman/p/4647590.html
Copyright © 2011-2022 走看看