代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
网络相关
作者:
壮壮
/ 发布于
2017/2/21
/
700
网络相关
package com.blankj.utilcode.utils; import android.content.Context; import android.content.Intent; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.telephony.TelephonyManager; import java.lang.reflect.Method; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.Socket; import java.net.SocketException; import java.net.UnknownHostException; import java.util.Enumeration; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; /** * <pre> * author: Blankj * blog : http://blankj.com * time : 2016/8/2 * desc : 网络相关工具类 * </pre> */ public class NetworkUtils { private NetworkUtils() { throw new UnsupportedOperationException("u can't instantiate me..."); } public enum NetworkType { NETWORK_WIFI, NETWORK_4G, NETWORK_3G, NETWORK_2G, NETWORK_UNKNOWN, NETWORK_NO } /** * 打开网络设置界面 * 3.0以下打开设置界面 */ public static void openWirelessSettings() { if (android.os.Build.VERSION.SDK_INT > 10) { Utils.getContext().startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); } else { Utils.getContext().startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); } } /** * 获取活动网络信息 * 需添加权限 {@code <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>} * * @return NetworkInfo */ private static NetworkInfo getActiveNetworkInfo() { return ((ConnectivityManager) Utils.getContext().getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo(); } /** * 判断网络是否连接 * 需添加权限 {@code <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>} * * @return {@code true}: 是{@code false}: 否 */ public static boolean isConnected() { NetworkInfo info = getActiveNetworkInfo(); return info != null && info.isConnected(); } /** * 判断网络是否可用 * 需添加权限 {@code <uses-permission android:name="android.permission.INTERNET"/>} * * @return {@code true}: 可用{@code false}: 不可用 */ public static boolean isAvailableByPing() { ShellUtils.CommandResult result = ShellUtils.execCmd("ping -c 1 -w 1 223.5.5.5", false); boolean ret = result.result == 0; if (result.errorMsg != null) { LogUtils.d("isAvailableByPing errorMsg", result.errorMsg); } if (result.successMsg != null) { LogUtils.d("isAvailableByPing successMsg", result.successMsg); } return ret; } /** * 判断移动数据是否打开 * * @return {@code true}: 是{@code false}: 否 */ public static boolean getDataEnabled() { try { TelephonyManager tm = (TelephonyManager) Utils.getContext().getSystemService(Context.TELEPHONY_SERVICE); Method getMobileDataEnabledMethod = tm.getClass().getDeclaredMethod("getDataEnabled"); if (null != getMobileDataEnabledMethod) { return (boolean) getMobileDataEnabledMethod.invoke(tm); } } catch (Exception e) { e.printStackTrace(); } return false; } /** * 打开或关闭移动数据 * 需系统应用 需添加权限{@code <uses-permission android:name="android.permission.MODIFY_PHONE_STATE"/>} * * @param enabled {@code true}: 打开{@code false}: 关闭 */ public static void setDataEnabled(boolean enabled) { try { TelephonyManager tm = (TelephonyManager) Utils.getContext().getSystemService(Context.TELEPHONY_SERVICE); Method setMobileDataEnabledMethod = tm.getClass().getDeclaredMethod("setDataEnabled", boolean.class); if (null != setMobileDataEnabledMethod) { setMobileDataEnabledMethod.invoke(tm, enabled); } } catch (Exception e) { e.printStackTrace(); } } /** * 判断网络是否是4G * 需添加权限 {@code <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>} * * @return {@code true}: 是{@code false}: 否 */ public static boolean is4G() { NetworkInfo info = getActiveNetworkInfo(); return info != null && info.isAvailable() && info.getSubtype() == TelephonyManager.NETWORK_TYPE_LTE; } /** * 判断wifi是否打开 * 需添加权限 {@code <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>} * * @return {@code true}: 是{@code false}: 否 */ public static boolean getWifiEnabled() { WifiManager wifiManager = (WifiManager) Utils.getContext().getSystemService(Context.WIFI_SERVICE); return wifiManager.isWifiEnabled(); } /** * 打开或关闭wifi * 需添加权限 {@code <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>} * * @param enabled {@code true}: 打开{@code false}: 关闭 */ public static void setWifiEnabled(boolean enabled) { WifiManager wifiManager = (WifiManager) Utils.getContext().getSystemService(Context.WIFI_SERVICE); if (enabled) { if (!wifiManager.isWifiEnabled()) { wifiManager.setWifiEnabled(true); } } else { if (wifiManager.isWifiEnabled()) { wifiManager.setWifiEnabled(false); } } } /** * 判断wifi是否连接状态 * 需添加权限 {@code <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>} * * @return {@code true}: 连接{@code false}: 未连接 */ public static boolean isWifiConnected() { ConnectivityManager cm = (ConnectivityManager) Utils.getContext() .getSystemService(Context.CONNECTIVITY_SERVICE); return cm != null && cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI; } /** * 判断wifi数据是否可用 * 需添加权限 {@code <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>} * 需添加权限 {@code <uses-permission android:name="android.permission.INTERNET"/>} * * @return {@code true}: 是{@code false}: 否 */ public static boolean isWifiAvailable() { return getWifiEnabled() && isAvailableByPing(); } /** * 获取网络运营商名称 * 中国移动、如中国联通、中国电信 * * @return 运营商名称 */ public static String getNetworkOperatorName() { TelephonyManager tm = (TelephonyManager) Utils.getContext().getSystemService(Context.TELEPHONY_SERVICE); return tm != null ? tm.getNetworkOperatorName() : null; } private static final int NETWORK_TYPE_GSM = 16; private static final int NETWORK_TYPE_TD_SCDMA = 17; private static final int NETWORK_TYPE_IWLAN = 18; /** * 获取当前网络类型 * 需添加权限 {@code <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>} * * @return 网络类型 * <ul> * <li>{@link NetworkUtils.NetworkType#NETWORK_WIFI } </li> * <li>{@link NetworkUtils.NetworkType#NETWORK_4G } </li> * <li>{@link NetworkUtils.NetworkType#NETWORK_3G } </li> * <li>{@link NetworkUtils.NetworkType#NETWORK_2G } </li> * <li>{@link NetworkUtils.NetworkType#NETWORK_UNKNOWN} </li> * <li>{@link NetworkUtils.NetworkType#NETWORK_NO } </li> * </ul> */ public static NetworkType getNetworkType() { NetworkType netType = NetworkType.NETWORK_NO; NetworkInfo info = getActiveNetworkInfo(); if (info != null && info.isAvailable()) { if (info.getType() == ConnectivityManager.TYPE_WIFI) { netType = NetworkType.NETWORK_WIFI; } else if (info.getType() == ConnectivityManager.TYPE_MOBILE) { switch (info.getSubtype()) { case NETWORK_TYPE_GSM: case TelephonyManager.NETWORK_TYPE_GPRS: case TelephonyManager.NETWORK_TYPE_CDMA: case TelephonyManager.NETWORK_TYPE_EDGE: case TelephonyManager.NETWORK_TYPE_1xRTT: case TelephonyManager.NETWORK_TYPE_IDEN: netType = NetworkType.NETWORK_2G; break; case NETWORK_TYPE_TD_SCDMA: case TelephonyManager.NETWORK_TYPE_EVDO_A: case TelephonyManager.NETWORK_TYPE_UMTS: case TelephonyManager.NETWORK_TYPE_EVDO_0: case TelephonyManager.NETWORK_TYPE_HSDPA: case TelephonyManager.NETWORK_TYPE_HSUPA: case TelephonyManager.NETWORK_TYPE_HSPA: case TelephonyManager.NETWORK_TYPE_EVDO_B: case TelephonyManager.NETWORK_TYPE_EHRPD: case TelephonyManager.NETWORK_TYPE_HSPAP: netType = NetworkType.NETWORK_3G; break; case NETWORK_TYPE_IWLAN: case TelephonyManager.NETWORK_TYPE_LTE: netType = NetworkType.NETWORK_4G; break; default: String subtypeName = info.getSubtypeName(); if (subtypeName.equalsIgnoreCase("TD-SCDMA") || subtypeName.equalsIgnoreCase("WCDMA") || subtypeName.equalsIgnoreCase("CDMA2000")) { netType = NetworkType.NETWORK_3G; } else { netType = NetworkType.NETWORK_UNKNOWN; } break; } } else { netType = NetworkType.NETWORK_UNKNOWN; } } return netType; } /** * 获取IP地址 * 需添加权限 {@code <uses-permission android:name="android.permission.INTERNET"/>} * * @param useIPv4 是否用IPv4 * @return IP地址 */ public static String getIPAddress(boolean useIPv4) { try { for (Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces(); nis.hasMoreElements(); ) { NetworkInterface ni = nis.nextElement(); // 防止小米手机返回10.0.2.15 if (!ni.isUp()) continue; for (Enumeration<InetAddress> addresses = ni.getInetAddresses(); addresses.hasMoreElements(); ) { InetAddress inetAddress = addresses.nextElement(); if (!inetAddress.isLoopbackAddress()) { String hostAddress = inetAddress.getHostAddress(); boolean isIPv4 = hostAddress.indexOf(':') < 0; if (useIPv4) { if (isIPv4) return hostAddress; } else { if (!isIPv4) { int index = hostAddress.indexOf('%'); return index < 0 ? hostAddress.toUpperCase() : hostAddress.substring(0, index).toUpperCase(); } } } } } } catch (SocketException e) { e.printStackTrace(); } return null; } /** * 获取域名ip地址 * 需添加权限 {@code <uses-permission android:name="android.permission.INTERNET"/>} * * @param domain 域名 * @return ip地址 */ public static String getDomainAddress(final String domain) { try { ExecutorService exec = Executors.newCachedThreadPool(); Future<String> fs = exec.submit(new Callable<String>() { @Override public String call() throws Exception { InetAddress inetAddress; try { inetAddress = InetAddress.getByName(domain); return inetAddress.getHostAddress(); } catch (UnknownHostException e) { e.printStackTrace(); } return null; } }); return fs.get(); } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } return null; } }
试试其它关键字
网络相关
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
壮壮
贡献的其它代码
(
8
)
.
mybatis如何遍历Map的key和value
.
udf使用间隔
.
获取 HTTP 服务器的信息
.
提取 HTML 文档中的链接和图片
.
网络相关
.
音频采集
.
字符串字串判断
.
模式匹配(kmp)
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3