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

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

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

 

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

1
2
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> 
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>

最后是MainActivity.java中的程序:

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

结果图:

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。