WifiInfo wifiInfo = null;
WifiManager wifiManager = null;
private Handler handler;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//获得WifiManager
wifiManager=(WifiManager) getSystemService(WIFI_SERVICE);
//使用定时器
Timer timer=new Timer();
timer.scheduleAtFixedRate(new TimerTask(){
@Override
public void run() {
Message msg=new Message();
msg.what=1;
handler.sendMessage(msg);
}
}, 1000, 5000);
//使用Handler实现UI线程与Timer线程之间的信息传递,每5秒告诉UI线程获得wifiInto
handler=new Handler(){
@Override
public void handleMessage(Message msg) {
switch(msg.what){
//如果收到正确的消息就获取WifiInfo
case 1: wifiInfo=wifiManager.getConnectionInfo();
System.out.println(wifiInfo.getRssi());break;
default:System.out.println("无信号");
}
}
};
}
需要manifest中权限
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />