欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 判断网络状态实例详解

程序员文章站 2022-05-07 11:33:26
android 判断网络状态实例详解 实例代码 package com.example.android; import java.io.ioexcep...

android 判断网络状态实例详解

实例代码

package com.example.android; 
 
import java.io.ioexception; 
import java.net.httpurlconnection; 
import java.net.inetaddress; 
import java.net.networkinterface; 
import java.net.socketexception; 
import java.net.url; 
import java.util.enumeration; 
 
import android.content.context; 
import android.net.connectivitymanager; 
import android.net.networkinfo; 
import android.telephony.telephonymanager; 
 
public class netstatus { 
 
  public static int net_cnnt_baidu_ok = 1; // 正常访问因特网状态 
  public static int net_cnnt_baidu_timeout = 2; // 无法访问因特网状态 
  public static int net_not_prepare = 3; // 网络未准备好 
  public static int net_error = 4; 
  private static int timeout = 3000; 
 
  /** 
   * 返回当前网络状态 
   * 
   * @param context 
   * @return 
   */ 
  public static int getnetstate(context context) { 
  try { 
    connectivitymanager connectivity = (connectivitymanager) context.getsystemservice(context.connectivity_service); 
    if (connectivity != null) { 
    networkinfo networkinfo = connectivity.getactivenetworkinfo(); 
    if (networkinfo != null) { 
      if (networkinfo.isavailable() && networkinfo.isconnected()) { 
      if (!connectionnetwork()) 
        return net_cnnt_baidu_timeout; 
      else 
        return net_cnnt_baidu_ok; 
      } else { 
      return net_not_prepare; 
      } 
    } 
    } 
  } catch (exception e) { 
  } 
  return net_error; 
  } 
 
  /** 
   * 拼百度地址 
   * 
   * @return 
   */ 
  private static boolean connectionnetwork() { 
  boolean result = false; 
  httpurlconnection httpurl = null; 
  try { 
    httpurl = (httpurlconnection) new url("http://www.baidu.com").openconnection(); 
    httpurl.setconnecttimeout(timeout); 
    httpurl.connect(); 
    result = true; 
  } catch (ioexception e) { 
  } finally { 
    if (null != httpurl) { 
    httpurl.disconnect(); 
    } 
    httpurl = null; 
  } 
  return result; 
  } 
 
  /** 
   * 判断当前网络是否是3g网络 
   * 
   * @param context 
   * @return boolean 
   */ 
  public static boolean is3g(context context) { 
  connectivitymanager connectivitymanager = (connectivitymanager) context.getsystemservice(context.connectivity_service); 
  networkinfo activenetinfo = connectivitymanager.getactivenetworkinfo(); 
  if (activenetinfo != null && activenetinfo.gettype() == connectivitymanager.type_mobile) { 
    return true; 
  } 
  return false; 
  } 
 
  /** 
   * 判断当前网络是否是wifi网络 
   * 
   * @param context 
   * @return boolean 
   */ 
  public static boolean iswifi(context context) { 
  connectivitymanager connectivitymanager = (connectivitymanager) context.getsystemservice(context.connectivity_service); 
  networkinfo activenetinfo = connectivitymanager.getactivenetworkinfo(); 
  if (activenetinfo != null && activenetinfo.gettype() == connectivitymanager.type_wifi) { 
    return true; 
  } 
  return false; 
  } 
 
  /** 
   * 判断当前网络是否是2g网络 
   * 
   * @param context 
   * @return boolean 
   */ 
  public static boolean is2g(context context) { 
  connectivitymanager connectivitymanager = (connectivitymanager) context.getsystemservice(context.connectivity_service); 
  networkinfo activenetinfo = connectivitymanager.getactivenetworkinfo(); 
  if (activenetinfo != null && (activenetinfo.getsubtype() == telephonymanager.network_type_edge 
      || activenetinfo.getsubtype() == telephonymanager.network_type_gprs  
      || activenetinfo.getsubtype() == telephonymanager.network_type_cdma)) { 
    return true; 
  } 
  return false; 
  } 
 
  /** 
   * wifi是否打开 
   */ 
  public static boolean iswifienabled(context context) { 
  connectivitymanager mgrconn = (connectivitymanager) context.getsystemservice(context.connectivity_service); 
  telephonymanager mgrtel = (telephonymanager) context.getsystemservice(context.telephony_service); 
  return ((mgrconn.getactivenetworkinfo() != null && mgrconn.getactivenetworkinfo().getstate() == networkinfo.state.connected)  
    || mgrtel.getnetworktype() == telephonymanager.network_type_umts); 
  } 
 
  /** 
   * 获得本机ip地址 
   * 
   * @return 
   */ 
  public static string gethostip() { 
  try { 
    for (enumeration<networkinterface> en = networkinterface.getnetworkinterfaces(); en.hasmoreelements();) { 
    networkinterface intf = en.nextelement(); 
    for (enumeration<inetaddress> ipaddr = intf.getinetaddresses(); ipaddr.hasmoreelements();) { 
      inetaddress inetaddress = ipaddr.nextelement(); 
      if (!inetaddress.isloopbackaddress()) { 
      return inetaddress.gethostaddress(); 
      } 
    } 
    } 
  } catch (socketexception ex) { 
  } catch (exception e) { 
  } 
  return null; 
  } 
 
  /** 
   * 获取本机串号imei 
   * 
   * @param context 
   * @return 
   */ 
  public static string getimei(context context) { 
  telephonymanager telephonymanager = (telephonymanager) context.getsystemservice(context.telephony_service); 
  return telephonymanager.getdeviceid(); 
  } 
} 

添加权限:访问网络权限

<uses-permission android:name="android.permission.access_network_state"/>  
<uses-permission android:name="android.permission.internet"/>  

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!