我试图在我的android应用程序中显示连接的wifi网络的信号强度。我试过以下方法:
//Receiver for information on the network info
private BroadcastReceiver mNetworkReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
Log.d("INFO", "onReceive()");
List<ScanResult> scanResult = mWifi.getScanResults();
for(ScanResult scan : scanResult)
{
Log.d("INFO","Network strength: " + String.valueOf(scan.level) + " dBm " + scan.SSID);
}
}
};
然后在onresume()/onpause()上注册/注销接收器:
@Override
public void onResume()
{
super.onResume();
mActivity.registerReceiver(mNetworkReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); //network info
}
@Override
public void onPause()
{
super.onPause();
mActivity.unregisterReceiver(mNetworkReceiver);
}
但这似乎行不通。我还启用了
ACCESS_WIFI_STATE
权限。只有当我手动调用<WiFiManager>.startScan()
时,才会调用我的接收器,但我正在尝试使它与广播接收器一起工作,以便每当信号强度发生变化时,我总能显示它。我注意到onReceive()
提供了一个列表,如果我只需要连接的网络的统计信息,这个列表是不必要的。有什么办法可以改变吗?编辑:还有,我知道这方面有一些相关的问题,但是我读过的那些问题并没有太大帮助。
编辑2:澄清了我的问题。主要问题是,除非我调用
startScan()
,否则永远不会调用我的接收器。 最佳答案
好吧,这段代码可以很好地与我一起使用toast来通知wifi信号强度,所以
我的接收器是这样的
@Override
public void onReceive(Context context, Intent intent) {
List<ScanResult> results = wifiDemo.wifi.getScanResults();
ScanResult bestSignal = null;
for (ScanResult result : results) {
if (bestSignal == null
|| WifiManager.compareSignalLevel(bestSignal.level, result.level) < 0)
bestSignal = result;
}
String message = String.format("%s networks found. %s is the strongest.",
results.size(), bestSignal.SSID);
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
Log.d("Debug", "onReceive() message: " + message);
}
这是我的启动活动中注册接收器并在textview上获取wifi信息的内容
public class WiFiDemo extends Activity {
WifiManager wifi;
BroadcastReceiver receiver;
TextView textStatus;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Setup UI
textStatus = (TextView) findViewById(R.id.textStatus);
// Setup WiFi
wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
// Get WiFi status
WifiInfo info = wifi.getConnectionInfo();
textStatus.append("\n\nWiFi Status: " + info.toString());
// List available networks
List<WifiConfiguration> configs = wifi.getConfiguredNetworks();
for (WifiConfiguration config : configs) {
textStatus.append("\n\n" + config.toString());
}
// Register Broadcast Receiver
if (receiver == null)
receiver = new WiFiScanReceiver(this);
registerReceiver(receiver, new IntentFilter(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
Log.d(TAG, "onCreate()");
}
@Override
public void onStop() {
unregisterReceiver(receiver);
super.onStop();
}
}
我在xml布局文件的textview上放置了scroll视图
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ScrollView
android:id="@+id/ScrollView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textStatus"
android:text="WiFi Connections :" />
</ScrollView>
</LinearLayout>
最后我的清单是这样的
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE">
</uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE">
</uses-permission>
<activity android:name=".WiFiDemo" 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=".WiFiScanReceiver">
<intent-filter>
<action android:name="com.rdc" />
</intent-filter>
</receiver>
还是有麻烦告诉我!!
关于android - 获取连接网络的WiFi信号强度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11389492/