• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps toggle wifi on/off when screen turns on/off

v1nsai

Member
Mar 24, 2010
60
3
I'm writing a simple app that will turn wifi on or off when the screen turns on or off, respectively. I tried registering <receiver> tags for SCREEN_ON and SCREEN_OFF Intents, then defining two BroadcastReceiver classes to catch the Intents, but nothing is happening. I think it's because the app isn't running when the screen turns on or off, which means I need a service running that can pass the Intents to the BroadcastReceivers, right?

Anyway, I'm having trouble figuring out how to connect the BroadcastReceivers to my service, how would I go about this?
 
Hi,
Good luck, i stumbled across the same issue. I'm trying to create an app that will allow me to configured settings like locale, but in a more flexible and dynamic way.

At the moment, its more for personal use than putting on the market.

Out of interest do you have any task-killer services running?

Its annoying that you can't really test such a scenario on the emulator.
 
Upvote 0
If your still having trouble, I got it figured out. If you always want Intents to be passed to your BroadcastReceiver as long as your app is installed, then use <receiver> and <intent-filter> tags in the manifest. If you only want to receive intents under certain curcumstances, use a service that dynamically registers the intentfilter objects in onCreate, and remove them in onDestroy.

Hope that helps!
 
Upvote 0
I'm still working on it, you can grab the source for it here

As of right now, the Wifi shuts off and turns back on no problem when screen turns on and off, but I'm working on getting the delays to work. Feel free to join the project!

Great work. Just one question......why include the option for a delay? Why wouldn't you want WiFi to turn off and on right away?
 
Upvote 0
Great work. Just one question......why include the option for a delay? Why wouldn't you want WiFi to turn off and on right away?

Its a matter of preference, really. The wifi will turn on the moment your screen turns on, even if your only checking the time real quick or something, i like a 5 second delay before wifi turns on. I like about a minute delay before it turns off, mainly to let my processes wrap up whatever they were doing, and if Im downloading something and accidentally let the screen turn off, I have a chance to catch it before I lose my connection
 
  • Like
Reactions: GIR
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones