博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android网络类型判断及IP地址获取
阅读量:4285 次
发布时间:2019-05-27

本文共 4135 字,大约阅读时间需要 13 分钟。

轉載自 

 

参考地址:

用户权限:

判断有无网络连接:

ConnectivityManager mConnectivity = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);   TelephonyManager mTelephony = (TelephonyManager)this.getSystemService(TELEPHONY_SERVICE);   //检查网络连接   NetworkInfo info = mConnectivity.getActiveNetworkInfo();     if (info == null || !mConnectivity.getBackgroundDataSetting()) {   return false;   }

判断WiFi是否已连接:

/**  * make true current connect service is wifi  * @param mContext  * @return  */  private static boolean isWifi(Context mContext) {      ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);      NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();      if (activeNetInfo != null && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {          return true;      }      return false;  }

判断WiFi和移动流量是否已连接:

public static boolean checkNetworkConnection(Context context)     {         final ConnectivityManager connMgr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);           final android.net.NetworkInfo wifi =connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);         final android.net.NetworkInfo mobile =connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);           if(wifi.isAvailable()||mobile.isAvailable())  //getState()方法是查询是否连接了数据网络             return true;         else             return false;     }

只判断移动网络连接是否正常:

ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);        NetworkInfo mMobileNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);   //获取移动网络信息      if (mMobileNetworkInfo != null) {            return mMobileNetworkInfo.isAvailable();    //getState()方法是查询是否连接了数据网络      }    }    return false;

一个工具类:

import java.net.InetAddress;  import java.net.NetworkInterface;  import java.net.SocketException;  import java.util.Enumeration;    import android.app.AlertDialog;  import android.content.Context;  import android.content.DialogInterface;  import android.content.Intent;  import android.net.ConnectivityManager;  import android.net.NetworkInfo;  import android.net.wifi.WifiInfo;  import android.net.wifi.WifiManager;  import android.provider.Settings;  import android.telephony.TelephonyManager;  import android.util.Log;    public class NetWorkUtils {        //  Context context;      public static boolean isNetWorkAvaliable(Context context){                    ConnectivityManager connect = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);          if(connect == null){              return false;          }else{              NetworkInfo[] info = connect.getAllNetworkInfo();              if(info != null){                  for(int i=0;i
wifiManager.setWifiEnabled(true); // } WifiInfo wifiInfo = wifiManager.getConnectionInfo(); int ipAddress = wifiInfo.getIpAddress(); String ip = intToIp(ipAddress); return ip; } private static String intToIp(int i) { return (i & 0xFF ) + "." + ((i >> 8 ) & 0xFF) + "." + ((i >> 16 ) & 0xFF) + "." + ( i >> 24 & 0xFF) ; } //使用GPRS public static String getLocalIpAddress() { try { for (Enumeration
en = NetworkInterface .getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration
enumIpAddr = intf .getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress()) { return inetAddress.getHostAddress().toString(); } } } } catch (SocketException ex) { Log.e("Exception", ex.toString()); } return "127.0.0.1"; } }

转载地址:http://ogsgi.baihongyu.com/

你可能感兴趣的文章
常用泰勒展开
查看>>
vector length_error
查看>>
Shell脚本处理浮点数的运算和比较实例
查看>>
bash shell for循环1到100
查看>>
latex中长公式换行,很好的办法
查看>>
nohup命令
查看>>
make 操作技巧指南--gcc版本设置
查看>>
sort和sortrows对矩阵排序
查看>>
matlab专区--------------matlab里面如何保留小数特定位数
查看>>
Matlab 绘图坐标轴刻度设置小数位数
查看>>
Matlab 条形图绘制 以及 添加误差棒 改变条形图形状
查看>>
cmake基本用法
查看>>
matlab 增加或减少图例 legend 线的长度
查看>>
matlab:把cell中的某个元素删去
查看>>
matlab 集合运算 交集 并集 差集
查看>>
C++ 给vector去重的三种方法
查看>>
map的详细用法
查看>>
C++初始化函数列表
查看>>
STL各种排序
查看>>
#include<map>
查看>>