码迷,mamicode.com
首页 > 移动开发 > 详细

自动刷新获取wifi信号强度,android

时间:2015-03-04 20:54:13      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:

我还是挺有必要把这次解决bug的过程记录下来的。转自http://fecbob.pixnet.net/blog/post/39322597-android-%3E-wifi-%3E-%E6%8E%83%E6%8F%8F%E9%99%84%E8%BF%91wifi-和http://stackoverflow.com/questions/8717741/application-never-receives-rssi-changed-action。这是我的第一个android程序,对于其中的一些细节原理,还需要再研究。

package com.example.wifi_scan;

import java.util.List;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends Activity {
/** Called when the activity is first created. */
private TextView wifiText;
private WifiManager wifiManager;
private StringBuilder sb;
private List<ScanResult> wifiList;
private WifiReceiver wifiReceiver;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

sb = new StringBuilder();
wifiText = (TextView) findViewById(R.id.wifi);
wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifiReceiver = new WifiReceiver();
registerReceiver(wifiReceiver, new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
wifiManager.startScan();
wifiText.setText("\nStarting Scan\n");


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 0, 0, "Refresh");
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
wifiManager.startScan();
wifiText.setText("Starting Scan");
return super.onMenuItemSelected(featureId, item);

}

@Override
protected void onPause() {
unregisterReceiver(wifiReceiver);
super.onPause();
}

@Override
protected void onResume() {
registerReceiver(wifiReceiver, new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
super.onResume();

WifiManager wifiMan = (WifiManager)getSystemService(Context.WIFI_SERVICE);
wifiMan.startScan();
}

private final class WifiReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

WifiManager wifiMan = (WifiManager)getSystemService(Context.WIFI_SERVICE);
wifiMan.startScan();

sb = new StringBuilder();
wifiList = wifiManager.getScanResults();
for (int i = 0; i < wifiList.size(); i++) {
sb.append(new Integer(i + 1).toString() + ".");
sb.append((wifiList.get(i)).toString()).append("\n\n");
}
wifiText.setText(sb.toString());
}
}


}

 

之前其实有挺多网上的代码是可以采集wifi信号强度的,但是没有找到能够实现刷新的,所以我也很纳闷该怎么做。然后自己找到一个可以用button按钮来刷新的,发现是可以刷新的,仔细对比了下,发现是有个函数在起作用startScan,但是无论我用timer,handler机制还是用receiver机制都不行,然后看到这篇博客http://blog.csdn.net/zd_1471278687/article/details/12158367下面的讨论,将SCAN_RESULTS_AVAILABLE_ACTION改成了RSSI_CHANGED_ACTION,但是发现状态还是没有变,接着我就继续Google了下wifimanager rssi changed action,然后找到了这个http://stackoverflow.com/questions/8717741/application-never-receives-rssi-changed-action,照着里面的方法,把receive改了下,发现就可以了!

从这个解决问题的路线,坚定了想法是:可以找人帮忙,但是最终解决问题的永远是自己!

 

自动刷新获取wifi信号强度,android

标签:

原文地址:http://www.cnblogs.com/hope100/p/4314218.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!