MainActivity如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package cc.testwifi; 
   
import android.os.Bundle; 
import android.app.Activity; 
/**
 * Demo描述:
 * 监听Wifi网络的变化并且获得当前信号强度
 
 *
 */ 
public class MainActivity extends Activity { 
   
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
    
}


WifiChangeBroadcastReceiver如下:

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
package cc.testwifi; 
   
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.net.wifi.WifiInfo; 
import android.net.wifi.WifiManager; 
   
public class WifiChangeBroadcastReceiver extends BroadcastReceiver { 
    private Context mContext; 
    @Override 
    public void onReceive(Context context, Intent intent) { 
        mContext=context; 
        System.out.println("Wifi发生变化"); 
        getWifiInfo(); 
    
       
    private void getWifiInfo() { 
        WifiManager wifiManager = (WifiManager) mContext.getSystemService(mContext.WIFI_SERVICE); 
        WifiInfo wifiInfo = wifiManager.getConnectionInfo(); 
        if (wifiInfo.getBSSID() != null) { 
            //wifi名称 
            String ssid = wifiInfo.getSSID(); 
            //wifi信号强度 
            int signalLevel = WifiManager.calculateSignalLevel(wifiInfo.getRssi(), 5); 
            //wifi速度 
            int speed = wifiInfo.getLinkSpeed(); 
            //wifi速度单位 
            String units = WifiInfo.LINK_SPEED_UNITS; 
            System.out.println("ssid="+ssid+",signalLevel="+signalLevel+",speed="+speed+",units="+units); 
        
   
   
}


AndroidManifest.xml如下:

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
<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="cc.testwifi" 
    android:versionCode="1" 
    android:versionName="1.0"
   
    <uses-sdk 
        android:minSdkVersion="10" 
        android:targetSdkVersion="10" /> 
       
    <uses-permission android:name="android.permission.INTERNET"/> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> 
   
    <application 
        android:allowBackup="true" 
        android:icon="@drawable/ic_launcher" 
        android:label="@string/app_name" 
        android:theme="@style/AppTheme"
        <activity 
            android:name="cc.testwifi.MainActivity" 
            android:label="@string/app_name"
            <intent-filter
                <action android:name="android.intent.action.MAIN" /> 
   
                <category android:name="android.intent.category.LAUNCHER" /> 
            </intent-filter
        </activity
           
        <receiver android:name="cc.testwifi.WifiChangeBroadcastReceiver"
            <intent-filter
                <action android:name="android.net.wifi.RSSI_CHANGED" />          
            </intent-filter
        </receiver
    </application
   
</manifest>

 

main.xml如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<RelativeLayout 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" 
    tools:context=".MainActivity"
   
    <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="监听wifi变化并获得当前信号强度"  
        android:layout_centerHorizontal="true" 
        android:layout_marginTop="50dip" 
    /> 
   
</RelativeLayout>
創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()