简体   繁体   中英

Change Wifi state on Android only if user changed

I have a program that scans and lists the phone's wifi periodically. With android version API >= 18 I can easily get those without turning on the wifi.

The problem, when the version is less than 18 is when the user:

  • Set wifi off: I have to turn wifi on, scan and then turn off again.
  • Set wifi on: I don't need to turn it off again.

I tried register a BroadcastReceiver that listens to WIFI_STATE_CHANGED_ACTION but how can I know if it was me that changed the wifi state or the user?

You can implement that kind of behavior by extending a BroadcastReceiver that will "listen" for custom events that you create.

No matter what you do, the system will broadcast the WIFI_STATE_CHANGED_ACTION , so what you're going to do is broadcast a custom intent that you create and have it captured by a BroadcastReceiver when the wifi state is changed from within your application.

Have the BroadcastReceiver listen only for your broadcast and then in the onReceive() method of the broadcast receiver activate the WiFi.

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