import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.net.NetworkInfo.State;
import android.telephony.TelephonyManager;
import android.util.Log; public class NetWorkHelper {
public static final int NETWORN_NONE = 0;
public static final int NETWORN_WIFI = 1;
public static final int NETWORN_MOBILE = 2;
private static String LOG_TAG = "NetWorkHelper"; public static Uri uri = Uri.parse("content://telephony/carriers"); /**
* 推断是否有网络
*/
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivity == null) {
Log.w(LOG_TAG, "couldn't get connectivity manager");
} else {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].isAvailable()) {
Log.d(LOG_TAG, "network is available");
return true;
}
}
}
}
Log.d(LOG_TAG, "network is not available");
return false;
}
/**
* 检查网络状态
* @param context
* @return
*/
public static boolean checkNetState(Context context){
boolean netstate = false;
ConnectivityManager connectivity = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
if(connectivity != null)
{
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++)
{
if (info[i].getState() == NetworkInfo.State.CONNECTED)
{
netstate = true;
break;
}
}
}
}
return netstate;
} /**
* 推断网络是否为漫游
*/
public static boolean isNetworkRoaming(Context context) {
ConnectivityManager connectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity == null) {
Log.w(LOG_TAG, "couldn't get connectivity manager");
} else {
NetworkInfo info = connectivity.getActiveNetworkInfo();
if (info != null
&& info.getType() == ConnectivityManager.TYPE_MOBILE) {
TelephonyManager tm = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
if (tm != null && tm.isNetworkRoaming()) {
Log.d(LOG_TAG, "network is roaming");
return true;
} else {
Log.d(LOG_TAG, "network is not roaming");
}
} else {
Log.d(LOG_TAG, "not using mobile network");
}
}
return false;
} /**
* 推断MOBILE网络是否可用
*
* @param context
* @return
* @throws Exception
*/
public static boolean isMobileDataEnable(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
boolean isMobileDataEnable = false; isMobileDataEnable = connectivityManager.getNetworkInfo(
ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting(); return isMobileDataEnable;
} /**
* 推断wifi 是否可用
* @param context
* @return
* @throws Exception
*/
public static boolean isWifiDataEnable(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
boolean isWifiDataEnable = false;
isWifiDataEnable = connectivityManager.getNetworkInfo(
ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();
return isWifiDataEnable;
} public static int getNetworkState(Context context){
ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); //Wifi
State state = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
if(state == State.CONNECTED||state == State.CONNECTING){
return NETWORN_WIFI;
} //3G
state = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
if(state == State.CONNECTED||state == State.CONNECTING){
return NETWORN_MOBILE;
}
return NETWORN_NONE;
} }

android工具类(2)NetWorkHelper 网络工具类的更多相关文章

  1. 从零开始学android开发-通过WebService进行网络编程,使用工具类轻松实现

    相信大家在平常的开发中,对网络的操作用到HTTP协议比较多,通过我们使用Get或者Post的方法调用一个数据接口,然后服务器给我们返回JSON格式的数据,我们解析JSON数据然后展现给用户,相信很多人 ...

  2. Android PermissionUtils:运行时权限工具类及申请权限的正确姿势

    Android PermissionUtils:运行时权限工具类及申请权限的正确姿势 ifadai 关注 2017.06.16 16:22* 字数 318 阅读 3637评论 1喜欢 6 Permis ...

  3. Android公共库——图片缓存 网络缓存 下拉及底部更多ListView 公共类

    Android公共库——图片缓存 网络缓存 下拉及底部更多ListView 公共类 转载自http://www.trinea.cn/android/android-common-lib/ 介绍总结的一 ...

  4. Android应用开发:网络工具——Volley(二)

    引言 在Android应用开发:网络工具--Volley(一)中结合Cloudant服务介绍了Volley的一般使用方法.当中包括了两种请求类型StringRequest和JsonObjectRequ ...

  5. c#实例化继承类,必须对被继承类的程序集做引用 .net core Redis分布式缓存客户端实现逻辑分析及示例demo 数据库笔记之索引和事务 centos 7下安装python 3.6笔记 你大波哥~ C#开源框架(转载) JSON C# Class Generator ---由json字符串生成C#实体类的工具

    c#实例化继承类,必须对被继承类的程序集做引用   0x00 问题 类型“Model.NewModel”在未被引用的程序集中定义.必须添加对程序集“Model, Version=1.0.0.0, Cu ...

  6. java中使用反射做一个工具类,来为指定类中的成员变量进行赋值操作,使用与多个类对象的成员变量的赋值。

    //------------------------------------------------我是代码的分割线 // 首选是一个工具类,在该工具类里面,定义了一个方法,public void s ...

  7. Redis操作Hash工具类封装,Redis工具类封装

    Redis操作Hash工具类封装,Redis工具类封装 >>>>>>>>>>>>>>>>>> ...

  8. Redis操作字符串工具类封装,Redis工具类封装

    Redis操作字符串工具类封装,Redis工具类封装 >>>>>>>>>>>>>>>>>>& ...

  9. Property工具类,Properties文件工具类,PropertiesUtils工具类

    Property工具类,Properties文件工具类,PropertiesUtils工具类 >>>>>>>>>>>>>& ...

随机推荐

  1. Oracle 数据库分页查询的三种方法

    一.Oracle 数据库分页查询的三种方法 1.简介 不能对 rownum 使用 >(大于或等于 1 的数值).>=(大于 1 的数值).=(不等于 1 的数值),否则无结果.所以直接用 ...

  2. Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---工厂模式之简单工厂

    简单工厂:工厂依据传进的参数创建相应的产品. http://www.cnblogs.com/DelphiDesignPatterns/archive/2009/07/24/1530536.html { ...

  3. Loj10222 佳佳的Fibonacci(矩阵乘法)

    题面 给定\(n,m\),求: \[ T(n)=\sum_{i=1}^ni\times f_i \] 其中\(f_i\)为斐波那契数列的第\(i\)项 题解 不妨设: \[ S(n)=\sum_{i= ...

  4. 图形管线之旅 Part 1

    原文:<A trip through the Graphics Pipeline 2011> 翻译:往昔之剑   转载请注明出处   你可以找到很多PC图形栈的功能描述,但是通常却不明所以 ...

  5. 【Go】windows下搭建go语言编译环境

    主要是协助杨哥做Kubernetes相关工作,由于Kubernetes和Docker都是由Go语言编写,因此改源码后还是需要go语言编译器来编译运行.所以打算先在windows上安装一下go语言环境. ...

  6. hdu 1011(Starship Troopers,树形dp)

    Starship Troopers Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...

  7. BZOJ 1131 [POI2008]Sta(树形DP)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1131 [题目大意] 给出一个N个点的树,找出一个点来,以这个点为根的树时,所有点的深度 ...

  8. Django contenttypes 框架详解

    一.什么是Django ContentTypes? Django ContentTypes是由Django框架提供的一个核心功能,它对当前项目中所有基于Django驱动的model提供了更高层次的抽象 ...

  9. iOS 自定义对象及子类及模型套模型的拷贝、归档存储的通用代码

    一.runtime实现通用copy 如果自定义类的子类,模型套模型你真的会copy吗,小心有坑. copy需要自定义类继承NSCopying协议 #import <objc/runtime.h& ...

  10. java.lang.RuntimeException: com.intellij.ide.plugins.PluginManager

    描述: 在mac电脑上的Android Studio.因为项目需求,加载plugins中的dart和Flutter插件.经过***后,依然无法从AS中加载进来. 曲折到Jetbrains官网下载了da ...