我正在开发一个应连接到网络并监控信号强度的 Android 应用程序。我已经做到了连接和看到力量,但我不知道如何积极地“听到”和展示力量。这是我的监控方法:
public void search(View v) {
// Turn on wifi
if (!wifi.isWifiEnabled()) {
if (wifi.getWifiState() != WifiManager.WIFI_STATE_ENABLING) {
wifi.setWifiEnabled(true);
}
}
// Register the desired network
int nId = wifi.addNetwork(netConfig);
// create the BroadcastReciever
if (wifiReciever == null) {
wifiReciever = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action
.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)) {
if (intent.getBooleanExtra(
WifiManager.EXTRA_SUPPLICANT_CONNECTED, false)) {
message.setText("Connected: "
+ wifi.getConnectionInfo().getRssi());
} else {
message.setText("Disconnected...");
}
} else if (action.equals(WifiManager.RSSI_CHANGED_ACTION)) {
message.setText("Connected: "
+ wifi.getConnectionInfo().getRssi());
}
}
};
}
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
intentFilter.addAction(WifiManager.RSSI_CHANGED_ACTION);
registerReceiver(wifiReciever, intentFilter);
// intentamos conectarnos
wifi.enableNetwork(nId, true);
}
此代码有时有效,但不会经常更新强度。是否有 API 或任何其他方法/隐藏的 API 来做我想做的事?
任何帮助都会很棒!