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

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

  1. flutter doctor出现问题 [!] Android toolchain - develop for Android devices (Android SDK version 28.0.3) X Android license status unknown. Try re-installing or updating your Android SDK Manager. 的解决方案

    首先,问题描述: flutter doctor Doctor summary (to see all details, run flutter doctor -v): [√] Flutter (Cha ...

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

    openstack里面的Provider network 和 Tenant network 的区别 openstack里面的网络相对复杂.经常有人对几个网络概念搞混淆,这里基本说明下 Openstac ...

  3. Error:java.util.concurrent.ExecutionException: com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details

    环境 Android Studio 3.0 升级&导入项目 错误 Error:java.util.concurrent.ExecutionException: com.android.tool ...

  4. com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details

    1.错误显示 com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details log提示:Generate Si ...

  5. 【解决】Failed to restart network.service: Unit network.service not found.

    问题:使用systemctl restart network 或 service network restart 命令重启网卡失败. 分析:原因其实也很简单,命令用错了,造成了找不到相应的网卡服务. ...

  6. android network develop(1)----doing network background

    Develop network with HttpURLConnection & HttpClient. HttpURLConnection  is lightweight with Http ...

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

    Normally, there are three type parser in android. Xmlpullparser, DOM & SAX. Google recomand Xmlp ...

  8. android 设置默认启动network mode

    network mode常见类型 WCDMA preferred : 0 GSM only : 1 WCDMA only : 2 GSM UMTS : 3 CDMA : 4 ... (参考RILCon ...

  9. [Keras] Develop Neural Network With Keras Step-By-Step

    简单地训练一个四层全连接网络. Ref: http://machinelearningmastery.com/tutorial-first-neural-network-python-keras/ 1 ...

随机推荐

  1. AngularJS的学习--TodoMVC的分析

    最近一段时间一直在看AngularJS,趁着一点时间总结一下. 官网地址:http://angularjs.org/ 先推荐几个教程 1. AngularJS入门教程 比较基础,是官方Tutorial ...

  2. Hadoop第1~2周练习—Hadoop1.X和2.X安装

        练习题目     Hadoop1.X安装 2.1    准备工作 2.1.1   硬软件环境 2.1.2   集群网络环境 2.1.3   安装使用工具 2.2  环境搭建 2.2.1   安 ...

  3. HMM 自学教程(三)隐藏模式

    本系列文章摘自 52nlp(我爱自然语言处理: http://www.52nlp.cn/),原文链接在 HMM 学习最佳范例,这是针对 国外网站上一个 HMM 教程 的翻译,作者功底很深,翻译得很精彩 ...

  4. extractCSS – 帮助你从 HTML 中快速分离出 CSS

    extractCSS 是一个免费的基于 Web 的应用程序,能够从 HTML 中提取风格相关的信息,包括 id.class 和内联样式,而且输出可以定制(缩进和括号的用法).该工具非常有用,当我们快速 ...

  5. 内存中OLTP(Hekaton)里的事务日志记录

    在今天的文章里,我想详细讨论下内存中OLTP里的事务日志如何写入事务日志.我们都知道,对于你的内存优化表(Memory Optimized Tables),内存中OLTP提供你2个持久性(durabi ...

  6. T4语法快速入门

    1.什么是T4? T4,即4个T开头的英文字母组合:Text Template Transformation Toolkit.T4(Text Template Transformation Toolk ...

  7. EFcodeFirst+T4=操纵任意数据库

    之前有写过两篇,EF选择Mysql数据源 跟 EF添加ADO.NET实体模型处直接选择Oracle数据源,其方便之处就不多说了,使用DBfirst直接点点点就能与数据库双向更新,而且关键是方便我们使用 ...

  8. Ubuntu搭建Android交叉编译环境

    一.下载 Android NDK Android NDK官方下载页:http://developer.android.com/tools/sdk/ndk/index.html如果需要旧版本的,比如10 ...

  9. Unity3D入门基本概念整理

    1. (1)在场景中添加资源 只需单击工程视图 (Project View) 中的网格(Mesh)并拖动至层级视图 (Hierarchy) 或场景视图 (Scene View),便可将其添加至场景 ( ...

  10. 使用layout_weight设置控件占屏幕百分比

    水平LinearLayout中如果A,B两个控件都是layout_weight="1",那么控件在水平方向占比为A的layout_width+1/2空闲空间,B的layout_wi ...