gpt4 book ai didi

android - 请求方连接更改不会触发

转载 作者:行者123 更新时间:2023-11-29 15:10:55 24 4
gpt4 key购买 nike

我想在 wifi 连接建立后做点什么。我有一个 BroadcastReceiver 可以完美地接收 NETWORK_STATE_CHANGED_ACTION 和 SCAN_RESULTS_AVAILABLE_ACTION,但不能接收 SUPPLICANT_CONNECTION_CHANGE_ACTION。这个更难测试:我为此关闭/打开路由器。

    protected void onCreate(Bundle savedInstanceState) {

receiverWifi = new WifiReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
intentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
registerReceiver(receiverWifi, intentFilter);
//...
}

class WifiReceiver extends BroadcastReceiver {
public void onReceive(Context c, Intent intent) {
final String action = intent.getAction();

Log.d("mhp","*BroadcastReceiver: " + action")}

还有 Manifiest.xml

  <application
a..
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.net.wifi.SCAN_RESULTS" />
<action android:name="android.net.wifi.supplicant.CONNECTION_CHANGE" />
<action android:name="android.net.wifi.STATE_CHANGE" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

AndroidManifest.xml:

  <application
a..
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.net.wifi.SCAN_RESULTS" />
<action android:name="android.net.wifi.supplicant.CONNECTION_CHANGE" />
<action android:name="android.net.wifi.STATE_CHANGE" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

最佳答案

连接到 wifi 网络,即使您没有收到 SUPPLICANT_CONNECTION_CHANGE_ACTION ,你一定会得到NETWORK_STATE_CHANGED_ACTION你可以玩这个 Action 来满足你的所有需求。

在广播接收器中,这样做:

String action = intent.getAction();
if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action))
{
NetworkInfo netInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
if ( (netInfo.getDetailedState()==(NetworkInfo.DetailedState.CONNECTED)) )
{
// your wifi is connected, do what you want to do
}
}

关于android - 请求方连接更改不会触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31596343/

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