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>

最新文章

  1. js获取键盘按下的键值event.keyCode,event.charCode,event.which的兼容性
  2. kali 密码攻击
  3. Win7 64位系统 VS2010连接Oracle报错的问题
  4. 正则表达式:re--python核心编程(3),chapter 1
  5. Android Studio Jar、so、library项目依赖
  6. Understanding the RelationshipType Enumeration [AX 2012]
  7. TableViewCell Swipe to Delete and More Button(like mail app in iOS7 or later)
  8. WPFDispatcher示例
  9. Singleton 模式
  10. Linux简介(好!)
  11. AngularJS 路由精分
  12. C语言嵌套循环
  13. MySQL创建用户与授权(CentOS6.5)
  14. 小程序md5加密
  15. VMWare14 安装Mac OS系统(图解)
  16. 泛微云桥e-Bridge安装手册
  17. go for range
  18. git push时提示&quot;Everything up-to-date&quot;
  19. Jython:java调用python文件之第三方包路径问题
  20. IOS是否在项目中存在,所使用的反射那点事

热门文章

  1. linux modprobe.conf怎么不见了—-CentOS 6
  2. linux下安装filezilla客户端遇到的问题
  3. ansible控制windows的官方翻译
  4. 干掉cmd:windows下使用linux命令行
  5. ps教程-三分钟画齿轮
  6. (转)Java关键字final、static使用总结
  7. 【脚本语言对比】BASH,PERL以及PYTHON
  8. Problem About Salesforce SOAP API 32.0 In .Net Project
  9. php--opp--2.什么是类,什么是对象,类和对象这间的关系
  10. 题目连接:http://acm.zznu.edu.cn/problem.php?id=1329