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

    }

}
时间: 2024-10-25 06:37:25

android network develop(2)----network status check的相关文章

RAD Studio 10 安装失败 提示错误 License status check failure解决办法

最近在安装RAD Studio 10.1 Berlin 时,运行光盘中的setup.exe,在第一页勾选 lincense agreement后下一步,提示如下错误,无法进行安装. License status check failure License status check failure (Error code: 255). Please try again later, or contact support. 通过一番搜索和尝试得到以下解决办法: 问题原因: 由于电脑中已经安装过其他版

2 Typical Methods of bridging IT Network and Control Network

While implementing MES projects, I always spent lots of time discussing how to bridge IT Network and Control Network. Practically, Network team will isolated Control Network from IT Network, such as using VLAN to separate them. MES works in IT Networ

Mac OS中, android stuido升级提示Connection failed. Please check your network connection and try again

在Mac系统中,安装android studio的正式版本是1.01的 如果直接在界面中点击check update,会弹出如下提示 Connection failed. Please check your network connection and try again 网传的一些方法过于繁琐,有一个临时替代的解决方案如下 1. Finder里, shift+command+g, 进入如下文件夹 /Applications/Android Studio.app/Contents/bin/stu

Android Studio升级提示 Connection failed. Please check your network ...

Android Studio升级 Android Studio出了稳定版,点击Check更新时,总是出现Connection failed. Please check your network ...无奈的很. 后来在网上查阅了类似的文章,这里来总结下升级失败提示 Connection failed. Please check your network ...解决方法,希望可以帮助大家. 关于这个问题,发现网上解决方法都差不多.本文章是在windows系统中的解决方法,如下修改: 在Androi

android network develop(3)----Xml Parser

Normally, there are three type parser in android. Xmlpullparser, DOM & SAX. Google recomand Xmlpullparser to doing this. But to parser xml files or inputstream, it based on xml content. So using an general way to parser xml. BaseXmlObj: /************

Android Studio升级提示 Connection failed. Please check 2

后来在网上查阅了类似的文章,这里来总结下升级失败提示 Connection failed. Please check your network ...解决方法,希望可以帮助大家. 关于这个问题,发现网上解决方法都差不多.本文章是在windows系统中的解决方法,如下修改: 在Android Studio安装目录"/bin/studio.exe.vmoptions"文件中追加以下几行: http://www.foooooot.com/footprint/2142071/ http://w

openstack里面的Provider network 和 Tenant network 的区别

openstack里面的网络相对复杂.经常有人对几个网络概念搞混淆,这里基本说明下 Openstack里面根据创建网络的用户的权限,Neutron network 可以分为: Provider network:管理员创建的和物理网络有直接映射关系的虚拟网络. Tenant network:租户普通用户创建的网络,其配置由 Neutorn 根据管理员在系统中的配置决定.受限于neutron配置. 根据网络的类型,Neutron network 可以分为: VLAN network(虚拟局域网) :

Android BLE开发之BluetoothGatt status 133

最近在做Android BLE开发,第一次接触蓝牙开发可以说遇到好多问题,好在都一一挺过来了! android的蓝牙开发遇到最常见的问题就是发现连接蓝牙设备连接不上,仔细一看竟然是 BluetoothGatt status 133,在Android开发这边经常出现这种情况,这是导致蓝牙设备连接不上的主要原因. 好多时候因为BluetoothGatt status 133,手机不得不重启才能重新连接上 最后终于找到缓解这种现象的办法android ble 133,解决办法就是要重新连接同一个蓝牙设

Android SDK:Android standard develop kits 安卓开发的工具集

目前主流的安卓开发工具: 1.Adnroid-Adt-bundle SDK Manager.exe: Tools(安卓的开发小工具) 各种安卓版本 Extras 额外的开发包 在线更新/安装的安卓版本的更新工具 不提倡在SDK Manager在线更新安卓版本.sdk: 主要用来存储安卓开发的环境 .android:虚拟机创建的文件 add-ons 存储google的一些API 平时很少 基本没使用 存储google地图 google paly 代码 build-tools 编译工具 docs 安