[英]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时是用户:
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.