Android wifi 信号强度的显示以及周边信号的代码

发布时间:2013-8-30 15:54    发布者:reggae
关键词: 信号强度 , Android , wifi
先把activity_main.xml文件代码贴出来.TextView充满屏幕
  1. 01
  2. 02
  3. xmlns:tools="http://schemas.android.com/tools"
  4. 03
  5. android:layout_width="match_parent"
  6. 04
  7. android:layout_height="match_parent"
  8. 05
  9. android:paddingBottom="@dimen/activity_vertical_margin"
  10. 06
  11. android:paddingLeft="@dimen/activity_horizontal_margin"
  12. 07
  13. android:paddingRight="@dimen/activity_horizontal_margin"
  14. 08
  15. android:paddingTop="@dimen/activity_vertical_margin"
  16. 09
  17. tools:context=".MainActivity" >
  18. 10
  19. 11
  20. 17
  21. 18
  22. android:id="@+id/textView1"
  23. 19
  24. android:layout_width="match_parent"
  25. 20
  26. android:layout_height="match_parent"
  27. 21
  28. android:gravity="center"
  29. 22
  30. android:textColor="@android:color/black"
  31. 23
  32. android:textSize="25sp" />
  33. 24
  34. 25
复制代码

AndroidMenifest.xml中,加入两行,作用:获取权限

最后是MainActivity.java中的程序:
查看源码打印?
  1. 01
  2. package com.example.wifistrength;
  3. 02
  4. 03
  5. import java.util.List;
  6. 04
  7. 05
  8. import android.net.wifi.ScanResult;
  9. 06
  10. import android.net.wifi.WifiInfo;
  11. 07
  12. import android.net.wifi.WifiManager;
  13. 08
  14. import android.os.Bundle;
  15. 09
  16. import android.app.Activity;
  17. 10
  18. import android.content.Context;
  19. 11
  20. import android.view.Menu;
  21. 12
  22. import android.widget.TextView;
  23. 13
  24. import android.widget.Toast;
  25. 14
  26. 15
  27. public class MainActivity extends Activity {
  28. 16
  29. 17
  30. TextView tv;
  31. 18
  32. 19
  33. @Override
  34. 20
  35. protected void onCreate(Bundle savedInstanceState) {
  36. 21
  37. super.onCreate(savedInstanceState);
  38. 22
  39. setContentView(R.layout.activity_main);
  40. 23
  41. 24
  42. String wserviceName = Context.WIFI_SERVICE;
  43. 25
  44. WifiManager wm = (WifiManager) getSystemService(wserviceName);
  45. 26
  46. 27
  47. WifiInfo info = wm.getConnectionInfo();
  48. 28
  49. int strength = info.getRssi();
  50. 29
  51. int speed = info.getLinkSpeed();
  52. 30
  53. String units = WifiInfo.LINK_SPEED_UNITS;
  54. 31
  55. String ssid = info.getSSID();
  56. 32
  57. 33
  58. tv  = (TextView) findViewById(R.id.textView1);
  59. 34
  60. 35
  61. List results = wm.getScanResults();
  62. 36
  63. String otherwifi = "The existing network is: \n\n";
  64. 37
  65. 38
  66. for (ScanResult result : results) {
  67. 39
  68. otherwifi += result.SSID  + ":" + result.level + "\n";
  69. 40
  70. }
  71. 41
  72. 42
  73. String text = "We are connecting to " + ssid + " at " + String.valueOf(speed) + "   " + String.valueOf(units) + ". Strength : " + strength;
  74. 43
  75. otherwifi += "\n\n";
  76. 44
  77. otherwifi += text;
  78. 45
  79. 46
  80. tv.setText(otherwifi);
  81. 47
  82. 48
  83. }
  84. 49
  85. 50
  86. 51
  87. @Override
  88. 52
  89. public boolean onCreateOptionsMenu(Menu menu) {
  90. 53
  91. // Inflate the menu; this adds items to the action bar if it is present.
  92. 54
  93. getMenuInflater().inflate(R.menu.main, menu);
  94. 55
  95. return true;
  96. 56
  97. }
  98. 57
  99. 58
  100. }
复制代码

结果图:


希望本文对广大安卓开发者有所帮助,感谢阅读本文。更多安卓技术问题欢迎加群探讨:278744577,验证码:eec,不写验证不予通过哟~


本文地址:https://www.eechina.com/thread-120145-1-1.html     【打印本页】

本站部分文章为转载或网友发布,目的在于传递和分享信息,并不代表本网赞同其观点和对其真实性负责;文章版权归原作者及原出处所有,如涉及作品内容、版权和其它问题,我们将根据著作权人的要求,第一时间更正或删除。
您需要登录后才可以发表评论 登录 | 立即注册

厂商推荐

相关视频

关于我们  -  服务条款  -  使用指南  -  站点地图  -  友情链接  -  联系我们
电子工程网 © 版权所有   京ICP备16069177号 | 京公网安备11010502021702
快速回复 返回顶部 返回列表