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. 链表中倒数第k个结点

    题目: 输入一个链表,输出该链表中倒数第k个结点. 思路: 因为是单向链表,如果使用最普通的遍历来解决的话会多出很多不必要的遍历.有一个比较好的解法,设置两个指针两个指针之间差k-1个位置,也就是当后 ...

  2. [数据库]sql之行顺序

    这个文章主要是防止我忘了sql的执行顺序,解释的东西我都没怎么看懂.数据库渣如我- 逻辑查询处理阶段简介 FROM:对FROM子句中的前两个表执行笛卡尔积(Cartesian product)(交叉联 ...

  3. Android学习笔记(第一篇)编写第一个程序Hello World+Activity

    PS:终于开始正式的搞Android了...无人带的一介菜鸟,我还是自己默默的努力吧... 学习内容: 1.编写第一个Hello World程序..   学习Android,那么就需要有一个编译器来集 ...

  4. html5中的大纲

    html5中的大纲 前言: 在html5中我们可以使用结构元素来编排一份大纲,这样我们就可以通过这个网页的大纲来了解网页中有哪些内容,网页中以什么样的形式来组织这些内容有更清楚的认识. 1.html5 ...

  5. 使用Spark分析拉勾网招聘信息(二): 获取数据

    要获取什么样的数据? 我们要获取的数据,是指那些公开的,可以轻易地获取地数据.如果你有完整的数据集,肯定是极好的,但一般都很难通过还算正当的方式轻易获取.单就本系列文章要研究的实时招聘信息来讲,能获取 ...

  6. 前端js的书写规范和高效维护的方案_自我总结使用的方案

    作为程序员,人生最值得幸福的事有几件: 解决困扰了很长时间的问题 升职加薪 找个漂亮又靠谱的对象 深得领导的喜欢 带领团队冲锋陷阵 ... 哈哈,这些都是梦想,暂时想想就好了.这肯定和我说的东西不符合 ...

  7. struts2基础——标签

    一.通用标签 1.s:property (读取值栈中对象的属性值) 属性:value:指定OGNL表达式:default:OGNL表达式返回为 null 时,使用默认值:escape:是否对 HTML ...

  8. Mysql的“Limit”操作

    Limit操作: ,; #返回第6-15行数据 ; #返回前5行 ,; #返回前5行 性能优化: 基于MySQL5.0中limit的高性能,我对数据分页也重新有了新的认识.测试SQL语句1: Sele ...

  9. OpenGL 圆角矩形

    本来打算用四个圆角GL_TRIANGLE_FANS+两个矩形来填充, 后来经无情公子的提醒, 突然发现:"尼玛就是一压扁了的圆啊!" 于是全部用GL_TRIANGLE_FANS, ...

  10. 微软发布ASP.NET 5路线图

    这次随 Visual Studio 2015 发布的 ASP.NET 版本是 ASP.NET 4.6 与 ASP.NET 5 beta5.在 VS2015 发布的同时,微软也发布了 ASP.NET 5 ...