简体   繁体   English

仅当用户更改时,才能在Android上更改Wifi状态

[英]Change Wifi state on Android only if user changed

I have a program that scans and lists the phone's wifi periodically. 我有一个程序会定期扫描并列出手机的wifi。 With android version API >= 18 I can easily get those without turning on the wifi. 使用android版本的API> = 18,我无需打开wifi即可轻松获得这些内容。

The problem, when the version is less than 18 is when the user: 问题是,当版本小于18时是用户:

  • Set wifi off: I have to turn wifi on, scan and then turn off again. 关闭wifi:我必须先打开wifi,然后扫描,然后再次关闭。
  • Set wifi on: I don't need to turn it off again. 开启wifi:我不需要再次将其关闭。

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? 我尝试注册一个监听WIFI_STATE_CHANGED_ACTION的BroadcastReceiver,但是如何知道是我改变了wifi状态还是用户呢?

You can implement that kind of behavior by extending a BroadcastReceiver that will "listen" for custom events that you create. 您可以通过扩展BroadcastReceiver来实现这种行为, BroadcastReceiver将“侦听” 创建的自定义事件。

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. 无论您做什么,系统都会广播WIFI_STATE_CHANGED_ACTION ,因此您要做的是广播您创建的自定义意图,并您的应用程序内部更改wifi状态时将其由BroadcastReceiver捕获。

Have the BroadcastReceiver listen only for your broadcast and then in the onReceive() method of the broadcast receiver activate the WiFi. BroadcastReceiver仅侦听您的广播,然后在广播接收器的onReceive()方法中激活WiFi。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 android.net.wifi.WIFI_STATE_CHANGED没有播出 - android.net.wifi.WIFI_STATE_CHANGED not being broadcast Android检查WIFI状态(断开连接或用户更改WIFI)如何标记FLAG? - Android check WIFI status (disconnected or user changed WIFI) How to FLAG it? Android 10:如何从Android系统通知中检测用户是否接受/拒绝了WIFI_STATE_CHANGE权限? - Android 10: how to detect if the user accepted/rejected the permission WIFI_STATE_CHANGE from the Android system notification? 要求android.permission.CHANGE_WIFI_STATE - Request android.permission.CHANGE_WIFI_STATE android wifi状态更改导致应用程序崩溃 - android wifi state change causes app crashed 检测屏幕关/开并在Android中更改wifi状态 - Detect screen off/on and change wifi state in Android android.net.wifi.STATE_CHANGE:在Wifi断开连接时未触发 - android.net.wifi.STATE_CHANGE: not triggered on Wifi disconnect 并非总是在wifi切换时触发android.net.wifi.STATE_CHANGE - android.net.wifi.STATE_CHANGE not always triggered on wifi handoff 如何使用SUPPLICANT_STATE_CHANGED_ACTION WiFi BroadcastReceiver-android - How to use SUPPLICANT_STATE_CHANGED_ACTION WiFi BroadcastReceiver - android 我在注册 BroadcastReceiver 后立即收到“android.net.wifi.WIFI_STATE_CHANGED” - I am receiving "android.net.wifi.WIFI_STATE_CHANGED" as soon as Registering BroadcastReceiver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM