android显示wifi信号强度以及周边信号(源代码)

先把activity_main.xml文件代码贴出来.TextView充满屏幕

[html]  view plain copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity" >  
  10.   
  11.  <!--       
  12.     <TextView  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:background="@android:color/white" />  
  16.  -->  
  17.     <TextView  
  18.         android:id="@+id/textView1"  
  19.         android:layout_width="match_parent"  
  20.         android:layout_height="match_parent"  
  21.         android:gravity="center"  
  22.         android:textColor="@android:color/black"  
  23.         android:textSize="25sp" />  
  24.   
  25. </LinearLayout>  

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

[html]  view plain copy
  1. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>  
  2. <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>  


最后是MainActivity.java中的程序:
[java]  view plain copy
  1. package com.example.wifistrength;  
  2.   
  3. import java.util.List;  
  4.   
  5. import android.net.wifi.ScanResult;  
  6. import android.net.wifi.WifiInfo;  
  7. import android.net.wifi.WifiManager;  
  8. import android.os.Bundle;  
  9. import android.app.Activity;  
  10. import android.content.Context;  
  11. import android.view.Menu;  
  12. import android.widget.TextView;  
  13. import android.widget.Toast;  
  14.   
  15. public class MainActivity extends Activity {  
  16.       
  17.     TextView tv;  
  18.   
  19.     @Override  
  20.     protected void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.activity_main);  
  23.           
  24.         String wserviceName = Context.WIFI_SERVICE;  
  25.         WifiManager wm = (WifiManager) getSystemService(wserviceName);   
  26.           
  27.         WifiInfo info = wm.getConnectionInfo();     
  28.         int strength = info.getRssi();  
  29.         int speed = info.getLinkSpeed();    
  30.         String units = WifiInfo.LINK_SPEED_UNITS;    
  31.         String ssid = info.getSSID();    
  32.           
  33.         tv  = (TextView) findViewById(R.id.textView1);  
  34.   
  35.         List<ScanResult> results = wm.getScanResults();  
  36.         String otherwifi = "The existing network is: \n\n";  
  37.           
  38.         for (ScanResult result : results) {    
  39.             otherwifi += result.SSID  + ":" + result.level + "\n";  
  40.         }  
  41.           
  42.         String text = "We are connecting to " + ssid + " at " + String.valueOf(speed) + "   " + String.valueOf(units) + ". Strength : " + strength;  
  43.         otherwifi += "\n\n";  
  44.         otherwifi += text;  
  45.           
  46.         tv.setText(otherwifi);  
  47.           
  48.     }  
  49.   
  50.   
  51.     @Override  
  52.     public boolean onCreateOptionsMenu(Menu menu) {  
  53.         // Inflate the menu; this adds items to the action bar if it is present.  
  54.         getMenuInflater().inflate(R.menu.main, menu);  
  55.         return true;  
  56.     }  
  57.       
  58. }  
结果图:

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值