gpt4 book ai didi

android - 广播接收器未检测到丢失的 wifi 连接

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

我在检测 wifi 变化时遇到了一些问题。我希望我的应用程序能够监控 wifi 网络并在 wifi 断开连接时创建一个警报对话框。在启动、恢复等时或 b。任何时候连接丢失。这是我的代码(我使用 this.registerReceiver(wifiStatusReceiver, new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION)) 在 onResume 中注册它);并在 onPause 中注销它:

 BroadcastReceiver wifiStatusReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int extraWifiState = intent.getIntExtra(
WifiManager.EXTRA_WIFI_STATE,
WifiManager.WIFI_STATE_UNKNOWN);

final String action = intent.getAction();

DialogFragment dialog = (DialogFragment) fragmentManager
.findFragmentByTag(WifiAlertDialogFragment.DIALOG_WIFI);

if (action.equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)) {
NetworkInfo info = (NetworkInfo) intent
.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
if (info.getState().equals(NetworkInfo.State.CONNECTED)) {
logger.d("wifi changed, is still connected");
if (dialog != null)
dialog.dismiss();
} else {
logger.d("connection lost");
if (dialog == null)
WifiAlertDialogFragment.wifiCheck(HomeActivity.this);
}
}

switch (extraWifiState) {

case WifiManager.WIFI_STATE_DISABLED:
WifiAlertDialogFragment.wifiCheck(HomeActivity.this);
break;
case WifiManager.WIFI_STATE_DISABLING:
Toast.makeText(HomeActivity.this, R.string.wifi_disabled,
Toast.LENGTH_SHORT).show();
WifiAlertDialogFragment.wifiCheck(HomeActivity.this);
break;
case WifiManager.WIFI_STATE_ENABLED:
if (dialog != null)
dialog.dismiss();
break;
case WifiManager.WIFI_STATE_ENABLING:
if (dialog != null)
dialog.dismiss();
Toast.makeText(HomeActivity.this, "Wifi Enabling",
Toast.LENGTH_LONG).show();
break;
case WifiManager.WIFI_STATE_UNKNOWN:
// no op
break;
}
}
};

最佳答案

更新:我发现了问题,所以我会自己回答这个问题。我只使用 SUPPLICANT_STATE_CHANGED_ACTION 因为它正在检查任何 wifi 状态——断开连接、禁用、连接、启用等。另一个对监视连接毫无用处,因为它只检查用户是否在设置中启用了 wifi还是不是。

注册我的接收器(在 onResume 中):

    IntentFilter filter = new IntentFilter(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
this.registerReceiver(wifiStatusReceiver, filter);

我的新代码如下所示:

BroadcastReceiver wifiStatusReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
logger.d("checking wifi state...");
SupplicantState supState;
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
supState = wifiInfo.getSupplicantState();
logger.d("supplicant state: " + supState);

SherlockDialogFragment dialog = (SherlockDialogFragment) fragmentManager
.findFragmentByTag(WifiAlertDialogFragment.DIALOG_WIFI);

if (supState.equals(SupplicantState.COMPLETED)) {
logger.d("wifi enabled and connected");
if (dialog != null)
dialog.dismiss();
} else {
WifiAlertDialogFragment.wifiCheck(HomeActivity.this);
if (supState.equals(SupplicantState.SCANNING)) {
logger.d("wifi scanning");
} else if (supState.equals(SupplicantState.DISCONNECTED)) {
logger.d("wifi disonnected");
} else {
Toast.makeText(HomeActivity.this, "Wifi Enabling",
Toast.LENGTH_LONG).show();
logger.d("wifi connecting");
}
}
}
};

关于android - 广播接收器未检测到丢失的 wifi 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11086797/

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