gpt4 book ai didi

android - 获取连接网络的 WiFi 信号强度

转载 作者:太空宇宙 更新时间:2023-11-03 12:00:10 27 4
gpt4 key购买 nike

我试图在我的 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()提供一个列表,如果我只想要连接网络的统计信息,则这是不必要的。有什么方法可以改变它吗?

编辑:另外,我意识到有一些相关的问题,但我读过的那些并没有太大帮助。edit2:稍微澄清了我的问题。主要问题是我的接收器永远不会被调用,除非我调用 startScan() .

最佳答案

好吧,当 WiFi 信号强度发生变化时,我可以使用 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);
}

这是在我的启动 Activity 中注册 Receiver 并在 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 布局文件中将 ScrollView 放在 TextView 上

<?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>

最后我的 list 是这样的

    <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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com