toggle wifi on/off when screen turns on/off


Last Updated:

  1. v1nsai

    v1nsai Well-Known Member This Topic's Starter

    Joined:
    Mar 24, 2010
    Messages:
    60
    Likes Received:
    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?
     

    Advertisement
  2. xathras

    xathras Member

    Joined:
    Apr 18, 2010
    Messages:
    17
    Likes Received:
    0
    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.
     
  3. v1nsai

    v1nsai Well-Known Member This Topic's Starter

    Joined:
    Mar 24, 2010
    Messages:
    60
    Likes Received:
    3
    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!
     
  4. omeshls

    omeshls New Member

    Joined:
    Jul 2, 2010
    Messages:
    2
    Likes Received:
    0
    Hi,
    Is this app complete now mate?... I really wanted to use it on my N1.. where i have to turn off and then turn on my wifi everytime i turn the screen on... if you app can do that automatically every time the screen turns on.. that would be great...
     
  5. v1nsai

    v1nsai Well-Known Member This Topic's Starter

    Joined:
    Mar 24, 2010
    Messages:
    60
    Likes Received:
    3
    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!
     
  6. nickf77

    nickf77 Well-Known Member

    Joined:
    Jul 16, 2010
    Messages:
    47
    Likes Received:
    3
    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?
     
  7. v1nsai

    v1nsai Well-Known Member This Topic's Starter

    Joined:
    Mar 24, 2010
    Messages:
    60
    Likes Received:
    3
    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
     
    GIR likes this.
  8. jonbonazza

    jonbonazza Well-Known Member

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
    Implement a Timer object and a TimerTask extended class.
     
  9. v1nsai

    v1nsai Well-Known Member This Topic's Starter

    Joined:
    Mar 24, 2010
    Messages:
    60
    Likes Received:
    3
    Yep, precisely what I did. All is working now, I need to update the google code repo when I get home.
     
  10. Dramarenko

    Dramarenko Active Member

    Joined:
    Oct 5, 2010
    Messages:
    32
    Likes Received:
    1

    Thanks for this app. You save me alot of battery! :rolleyes:

    how can i start this app as service? I don't want to open this app manually every time that i reboot my device :)
     
  11. GIR

    GIR Well-Known Member

    Joined:
    Feb 14, 2010
    Messages:
    96
    Likes Received:
    10
    Hello,
    I just clocked this, what would need to be changed to do control the GPS in the same manner?

    Regards,
    Fred
     

Share This Page

Loading...