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();
}
} } }
android network develop(2)----network status check的更多相关文章
- 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 ...
- openstack里面的Provider network 和 Tenant network 的区别
openstack里面的Provider network 和 Tenant network 的区别 openstack里面的网络相对复杂.经常有人对几个网络概念搞混淆,这里基本说明下 Openstac ...
- 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 ...
- 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 ...
- 【解决】Failed to restart network.service: Unit network.service not found.
问题:使用systemctl restart network 或 service network restart 命令重启网卡失败. 分析:原因其实也很简单,命令用错了,造成了找不到相应的网卡服务. ...
- android network develop(1)----doing network background
Develop network with HttpURLConnection & HttpClient. HttpURLConnection is lightweight with Http ...
- android network develop(3)----Xml Parser
Normally, there are three type parser in android. Xmlpullparser, DOM & SAX. Google recomand Xmlp ...
- android 设置默认启动network mode
network mode常见类型 WCDMA preferred : 0 GSM only : 1 WCDMA only : 2 GSM UMTS : 3 CDMA : 4 ... (参考RILCon ...
- [Keras] Develop Neural Network With Keras Step-By-Step
简单地训练一个四层全连接网络. Ref: http://machinelearningmastery.com/tutorial-first-neural-network-python-keras/ 1 ...
随机推荐
- Hadoop入门进阶课程5--MapReduce原理及操作
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,博主为石山园,博客地址为 http://www.cnblogs.com/shishanyuan ...
- Android Http请求
Android HTTP请求封装代码 /** * This class is the Utils of other classes. */ public class HttpUtil { /** 变量 ...
- 我也想聊聊 OAuth 2.0 —— 基本概念
这是一篇待在草稿箱半年之久的文章 连我自己都不知道我的草稿箱有多少未发布的文章了.这应该是我在上一家公司未解散之前写的,记得当时是要做一个开发者中心,很不幸. 今天,打开草稿箱有种莫名的伤感,看到这个 ...
- 转载:全球首个微信小程序(应用号)开发教程!通宵吐血赶稿,每日更新!
微信应用号(小程序,「应用号」的新称呼)终于来了! 目前还处于内测阶段,微信只邀请了部分企业参与封测.想必大家都关心应用号的最终形态到底是什么样子?怎样将一个「服务号」改造成为「小程序」? 我们暂时以 ...
- python使用SMTP发送邮件
SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式.SMTP协议属于TCP/IP协议簇,它 ...
- 【C#进阶系列】06 类型和成员基础
这些东西是基础中的基础,基本上是本书都会讲这个.但是很多东西到处都有,所以只捡了以下的这些写下来. 关于类型的可见性和可访问性 也就是public,internal这种东西,但是还是有个东西要提一下, ...
- Winform调用QQ发信息并且开机启动 (开源)
前言 公司CS系统需要加入启动qq从winform调用qq聊天窗口的功能,前提是需要将聊天者的QQ号码作为参数传递到函数中,一直没有搞过,正好很感兴趣,就折腾,Winform调用qq,我想肯定是需要一 ...
- FreeBSD暂时用9.X系列为宜
今天尝试在FreeBSD10 上编译c代码,发现gcc被换成llvm后,环境配置需要重新学习.
- php实现添加图片水印
实际运行时需要开启php 的gd2功能,运行环境php4.0以上(demo中的路径改为实际路径) <?php/*打开图片*/ //1.配置图片路径 $src="image/61.jpg ...
- oop典型应用:实体类
1.什么是实体类 简单地说就是描述一个业务实体的“类”,业务实体直观一点理解就是整个就是整个软件系统业务所涉及的对象. eg:MySchool系统中的班级,学生,年级等都是业务实体,“雷电”游戏中的飞 ...