• Android监听WIFI网络的变化并且获得当前信号强度


    MainActivity如下:

    package cc.testwifi;
    
    import android.os.Bundle;
    import android.app.Activity;
    /**
     * Demo描述:
     * 监听Wifi网络的变化并且获得当前信号强度
     * 
     * 参考资料:
     * http://blog.csdn.net/way_ping_li/article/details/8777478
     * Thank you very much
     *
     */
    public class MainActivity extends Activity {
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.main);
    	}
    }
    


    WifiChangeBroadcastReceiver如下:

    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如下:

    <?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如下:

    <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>
  • 相关阅读:
    Linux 线程的互斥和等待 pthread_mutex_lock/pthread_mutex_unlock pthread_cond_wait/pthread_cond_signal
    Ubuntu12.10 双显卡过热问题
    Ubuntu apt-get 无法使用的问题
    Unload Java JNI DLL
    python笔记(19)--面向对象三大特性补充
    python笔记(18)--类的命名空间、实例化绑定方法和类的组合用法
    python笔记(17)--初识面向对象和三大特性(封装/继承/多态)
    python笔记(16)--迭代器、生成器和logging模块
    python笔记(15)--模块扩展(time/json/shutil/logging)和异常捕获处理
    python笔记(14)--内置模块(json重要)
  • 原文地址:https://www.cnblogs.com/suncoolcat/p/3395461.html
Copyright © 2020-2023  润新知