简体   繁体   中英

android.net.wifi.STATE_CHANGE intent handling (in Activity)

I would like to received a notification of the change of state of WiFi connection in my Android application. I found several suggestion (here on SO) that mostly leads to the definition of an intent. I'm using this approach:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_controller);

    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
    registerReceiver(broadcastReceiver, intentFilter);

    checkWiFi();
}

public BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent != null)
            checkWiFi();
    }
};

That works but I'm wondering if I can use a Manifest centric approach ie if I put this in my AndroidManifest.xml :

    <activity
        android:name=".MainActivity"
        android:screenOrientation="landscape">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.net.wifi.STATE_CHANGE" />
        </intent-filter>
    </activity>

I should be notified of the incoming intent . Right? How can I handle this in my Activity?

Best regards, Mike

You should create class MyBroadcastReceiver extends BroadcastReceiver

and next Manifest

<receiver android:name=".MyBroadcastReceiver">
    <intent-filter>
        <action android:name="android.net.wifi.STATE_CHANGE" />
    </intent-filter>
</receiver>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM