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 ...
随机推荐
- 15套精美的免费界面设计 PSD 素材【免费下载】
在这个集合中,我们聚集15套精美的 PSD 界面设计模板,网页元素,用户界面工具包,扁平化图标,APP 应用程序 UI 设计的等等.这些来自优秀设计师的 PSD 源文件素材让其它的设计师们在设计用 ...
- Fiddler进行手机抓包
测试设备:Windows8.1,Android 4.0.4山寨Android手机 网络环境:电信校园网,Wifi环境 测试工具:Fiddler4 完全参考了:http://www.jb51.net/s ...
- C#继承基本控件实现自定义控件
C#继承基本控件实现自定义控件 摘自:http://www.cnblogs.com/greatverve/archive/2012/04/25/user-control-inherit.html 自定 ...
- div与>div区别小结
两者之间的区别:例如div span得到的是div下所有的span元素,而div>span则是取得的div下第一级的span元素. 示例代码如下: <!DOCTYPE html> & ...
- 【jQuery基础学习】01 jQuery选择器
关于CSS选择器 jQuery选择器涉及到CSS,CSS技术使得网页的结构与表现样式完全分离. 同样CSS也需要找到某个网页的结构才能改变其样式,这就是CSS选择器. 常用的CSS选择器如下: 标签选 ...
- [PE结构分析] 5.IMAGE_OPTIONAL_HEADER
结构体源代码如下: typedef struct _IMAGE_OPTIONAL_HEADER { // // Standard fields. // +18h WORD Magic; // 标志字, ...
- Hibernate框架之Criteria查询
首先给大家说说Hibernate检索方式 Hibernate提供了5种检索对象的方式 1.导航对象图检索方式:根据已经加载的对象导航到其他对象 2.OID检索方式:按照对象的OID来检索对象 3.HQ ...
- [小北De编程手记] : Lesson 08 - Selenium For C# 之 PageFactory & 团队构建
本文想跟大家分享的是Selenium对PageObject模式的支持和自动化测试团队的构建.<Selenium For C#>系列的文章写到这里已经接近尾声了,如果之前的文章你是一篇篇的读 ...
- ASP.NET Web API 特性
ASP.NET MVC 4 包含了 ASP.NET Web API, 这是一个创建可以连接包括浏览器.移动设备等多种客户端的 Http 服务的新框架, ASP.NET Web API 也是构建 RES ...
- Eclipse下Android开发的问题:Failed to install AndroidPhone.apk on device 'emulator-5554': timeout 解决办法
在window->preferences->Android->DDMS->ADB connection time out (ms): 将这个值设置的大一些,默认为5000,我设 ...