请求方连接更改不会触发

Sor*_*tum 1 android broadcastreceiver android-wifi

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")}
Run Code Online (Sandbox Code Playgroud)

和 Manifyingt.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>
Run Code Online (Sandbox Code Playgroud)

和 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>
Run Code Online (Sandbox Code Playgroud)

unr*_*007 5

On connecting to wifi network even if you don't get SUPPLICANT_CONNECTION_CHANGE_ACTION, you will definitely get NETWORK_STATE_CHANGED_ACTION and you can play with this action for all your needs.

In the broadcast receiver, do this:

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
    }
}
Run Code Online (Sandbox Code Playgroud)