Android wifi 信號強度的顯示以及周邊信號的代碼

發(fā)布時(shí)間:2013-8-30 15:54    發(fā)布者:reggae
關(guān)鍵詞: 信號強度 , Android , wifi
先把activity_main.xml文件代碼貼出來(lái).TextView充滿(mǎn)屏幕
  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. }
復制代碼

結果圖:


希望本文對廣大安卓開(kāi)發(fā)者有所幫助,感謝閱讀本文。更多安卓技術(shù)問(wèn)題歡迎加群探討:278744577,驗證碼:eec,不寫(xiě)驗證不予通過(guò)喲~


本文地址:http://selenalain.com/thread-120145-1-1.html     【打印本頁(yè)】

本站部分文章為轉載或網(wǎng)友發(fā)布,目的在于傳遞和分享信息,并不代表本網(wǎng)贊同其觀(guān)點(diǎn)和對其真實(shí)性負責;文章版權歸原作者及原出處所有,如涉及作品內容、版權和其它問(wèn)題,我們將根據著(zhù)作權人的要求,第一時(shí)間更正或刪除。
您需要登錄后才可以發(fā)表評論 登錄 | 立即注冊

相關(guān)視頻

關(guān)于我們  -  服務(wù)條款  -  使用指南  -  站點(diǎn)地圖  -  友情鏈接  -  聯(lián)系我們
電子工程網(wǎng) © 版權所有   京ICP備16069177號 | 京公網(wǎng)安備11010502021702
快速回復 返回頂部 返回列表
午夜高清国产拍精品福利|亚洲色精品88色婷婷七月丁香|91久久精品无码一区|99久久国语露脸精品|动漫卡通亚洲综合专区48页