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"/>
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
上一篇: Android之有效防止按钮多次重复点击的方法(必看篇)
下一篇: 挑杏鲍菇有何方法
推荐阅读
-
Android TextView Marquee的应用实例详解
-
Android网络技术HttpURLConnection详解
-
android短信管理器SmsManager实例详解
-
Android 中Lambda表达式的使用实例详解
-
Android ViewPagerIndicator详解及实例代码
-
android判断设备是否有相机的实例代码
-
Android camera2 判断相机功能是否可控的实例
-
Android Kotlin开发实例(Hello World!)及语法详解
-
Android Fragment滑动组件ViewPager的实例详解
-
Android ListView中动态添加RaidoButton的实例详解