注意!是判断网络是否可用,但网络可用不代表一定能上外网的! 在AndroidManifest.xml文件下,添加下面权限: 如果想判断是否能上外网可以ping一下外网的ip地址: 比如ping一下qq.com: 在AndroidManifest.xml文件下,添加下面权限: 在AndroidManifest.xml文件下,添加下面权限:
判断网络是否可用:
public static boolean isNetworkAvailable(Context context) { ConnectivityManager manager = (ConnectivityManager) context .getApplicationContext().getSystemService( Context.CONNECTIVITY_SERVICE); if (manager == null) { return false; } NetworkInfo networkInfo = manager.getActiveNetworkInfo(); return networkInfo != null && networkInfo.isConnected(); }
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
public static boolean pingIPAddress(String ipAddress) { try { //-c 1是指ping的次数为1次,-w 3是指超时时间为3s Process process = Runtime.getRuntime() .exec("ping -c 1 -w 3 " + ipAddress); //status为0表示ping成功 int status = process.waitFor(); if (status == 0) { return true; } }catch (InterruptedException | IOException e) { e.printStackTrace(); } return false; }
pingIPAddress("qq.com");
<uses-permission android:name="android.permission.INTERNET"/>
获取IP地址:
public static String getHostIPAddress() { String IPAddress = null; try { Enumeration nis = NetworkInterface.getNetworkInterfaces(); while (nis.hasMoreElements()) { NetworkInterface ni = (NetworkInterface) nis.nextElement(); Enumeration<InetAddress> ias = ni.getInetAddresses(); while (ias.hasMoreElements()) { InetAddress ia = ias.nextElement(); if (ia instanceof Inet6Address) { continue; } String hostAddress = ia.getHostAddress(); if (!"127.0.0.1".equals(hostAddress)) { IPAddress = hostAddress; break; } } } } catch (SocketException e) { e.printStackTrace(); } return IPAddress; }
<uses-permission android:name="android.permission.INTERNET"/>
获取以太网口MAC地址:
public static String getEthernetMacAddress() { BufferedReader reader = null; FileReader fr = null; String ethernetMacAddress = null; try { fr = new FileReader("sys/class/net/eth0/address"); reader = new BufferedReader(fr); ethernetMacAddress = reader.readLine(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (reader != null) reader.close(); if (fr != null) fr.close(); } catch (IOException e) { e.printStackTrace(); } } return ethernetMacAddress; }
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算