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();            }        }            }}

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. Android 面试总结
  2. Android开发错误信息收集
  3. 浅谈Android开源工具篇
  4. android 从驱动到应用(二)
  5. Android(安卓)截图和录屏
  6. Android(安卓)1分钟教你打造酷炫的引导页
  7. Android Widget 小工具(两) 使用configur
  8. Android studio assets error:前言中不允
  9. Ubuntu下android源码下载
  10. Android 说说亮屏锁和键盘锁