首页 > 系统 > Android > 正文

Android编程实现获取当前连接wifi名字的方法

2019-10-24 20:34:11
字体:大 中 小
来源:转载
供稿:网友

这篇文章主要介绍了Android编程实现获取当前连接wifi名字的方法,涉及Android针对WiFi属性操作的相关技巧,需要的朋友可以参考下

本文实例讲述了Android编程实现获取当前连接wifi名字的方法。分享给大家供大家参考,具体如下:

 

  1. WifiManager wifiMgr = (WifiManager) mActivity.getSystemService(Context.WIFI_SERVICE); 
  2. int wifiState = wifiMgr.getWifiState(); 
  3. WifiInfo info = wifiMgr.getConnectionInfo(); 
  4. String wifiId = info != null ? info.getSSID() : null; 
  5. public static InetAddress getWifiIp() { 
  6. Context myContext = Globals.getContext(); 
  7. if (myContext == null) { 
  8. throw new NullPointerException("Global context is null"); 
  9. } 
  10. WifiManager wifiMgr = (WifiManager) myContext.getSystemService(Context.WIFI_SERVICE); 
  11. if (isWifiEnabled()) { 
  12. int ipAsInt = wifiMgr.getConnectionInfo().getIpAddress(); 
  13. if (ipAsInt == 0) { 
  14. return null; 
  15. } else { 
  16. return Util.intToInet(ipAsInt); 
  17. } 
  18. } else { 
  19. return null; 
  20. } 
  21. } 
  22. // 取得wifi的ip地址 
  23. InetAddress address = FTPServerService.getWifiIp(); 
  24. address.getHostAddress(); 
  25. public static boolean isWifiEnabled() { 
  26. Context myContext = Globals.getContext(); 
  27. if (myContext == null) { 
  28. throw new NullPointerException("Global context is null"); 
  29. } 
  30. WifiManager wifiMgr = (WifiManager) myContext.getSystemService(Context.WIFI_SERVICE); 
  31. if (wifiMgr.getWifiState() == WifiManager.WIFI_STATE_ENABLED) { 
  32. ConnectivityManager connManager = (ConnectivityManager) myContext 
  33. .getSystemService(Context.CONNECTIVITY_SERVICE); 
  34. NetworkInfo wifiInfo = connManager 
  35. .getNetworkInfo(ConnectivityManager.TYPE_WIFI); 
  36. return wifiInfo.isConnected(); 
  37. } else { 
  38. return false; 
  39. } 
  40. } 
  41. // 打开wifi设置的页面 
  42. Intent intent = new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS); 
  43. startActivity(intent); 

希望本文所述对大家Android程序设计有所帮助。


注:相关教程知识阅读请移步到Android开发频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表