PowerManager and WakeLock


Last Updated:

  1. miXer

    miXer Well-Known Member This Topic's Starter

    Joined:
    Aug 12, 2010
    Messages:
    313
    Likes Received:
    51
    Hey.

    I'm developing an sms receiver, and i have included a KeyguardManager and a PowerManager to be able to disable the keyguard and turn on the screen if user would like to. This works great, but only once.

    When a message is received, the keyguard gets unlocked and the screen is turned on, and it gives out a sound and vibration. But if i don't read the message right away, but let the device power off by screen time out, and i then receive another message while the last one is not read, the device does not turn the screen on again, and it does not give out a sound or vibration, until i turn on the screen manually, then the sound and vibration comes.

    Why?

    If i close all received messages, then my app is able to disable the keyguard and turn on the screen and give out a sound and vibration when a new message is received.

    Here is my code that is run when the keyguard should be unlocked and the screen should be turned on.
    Code (Text):
    1. private void disableKeyGuard()
    2.     {
    3.         KeyguardManager km = (KeyguardManager)getSystemService(KEYGUARD_SERVICE);
    4.         kl = km.newKeyguardLock("SMSHandler");
    5.         kl.disableKeyguard();
    6.         km.exitKeyguardSecurely(null);
    7.     }
    8.  
    9.     private void turnOnScreen()
    10.     {
    11.         PowerManager pm = (PowerManager)getSystemService(POWER_SERVICE);
    12.         wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP|PowerManager.SCREEN_DIM_WAKE_LOCK|PowerManager.ON_AFTER_RELEASE, "SMSHandler");
    13.         wl.acquire();
    14.         wl.release();
    15.     }
     

    Advertisement
  2. RNekic

    RNekic Member

    Joined:
    May 25, 2010
    Messages:
    12
    Likes Received:
    1
    It seems like you should look at different code, not the code you posted. It sounds like the code you've posted works fine, it's just not getting called in the situation you describe.

    I've never played with the SMS stuff but if you get a SMS and leave it unacknowledged in any way, perhaps the phone does not send the intent/broadcast again or a different type of notification occurs when another SMS comes in?
     
  3. miXer

    miXer Well-Known Member This Topic's Starter

    Joined:
    Aug 12, 2010
    Messages:
    313
    Likes Received:
    51
    Thanks for your reply, but i have checked all my other code.
    I have tried not to disable the keyguard and not turn on the screen, and then the phone gives out both sound and vibration if more then one message arrives.

    If a message is allready showing and another message is received, a new broadcast/intent is sent, and is received in a method called onNewInten(), so this works great, its just when my app disables the keyguard and turns the screen on and the phone goes back to sleep i get trouble. And i don't know why.
     
  4. RNekic

    RNekic Member

    Joined:
    May 25, 2010
    Messages:
    12
    Likes Received:
    1
    How are you handling onNewIntent()?
     
  5. miXer

    miXer Well-Known Member This Topic's Starter

    Joined:
    Aug 12, 2010
    Messages:
    313
    Likes Received:
    51
    I have a method called doNotify() which is called in onCreate() when the first message is received, and doNotify() is also called in onNewIntent() if another message is received while the previous is still showing.

    doNotify() calls disableKeyGuard() and turnOnScreen() and another method which makes a Notification in the notification bar.

    But the weird thing is, both methods onCreate() and onNewIntent() calls the same method doNotify() each time a message is received. And doNotify() does the same thing for each of them. So the problem must be in how i use the Keyguard manager and the power manager.

    If i choose to not disable keyguard and not turn on screen, and my app receives two messages or more, the only thing my app does'nt call is disableKeyGuard() and turnOnScreen(), else it's the same procedure.
     
  6. miXer

    miXer Well-Known Member This Topic's Starter

    Joined:
    Aug 12, 2010
    Messages:
    313
    Likes Received:
    51
    Okey, now i know where the problem is, and it's for sure the wake lock.

    I made my app write to a file on the sd card when certain lines where run. And here is what i found out:

    1. When a message is received, and no other message is showing, the onCreate() is called. And this calls the doNotify() which then again calls disableKeyGuard() and turnOnScreen() and giveNotification(), no mater what state the device is in.

    2. When another message is received, while the previous is showing, and the device is locked and the screen is turned off, the onNewIntent() is not called until i manually turn on the screen. Then the message is shown, and the notification is given.

    3. When another message is received, while the previous is showing, and the device is locked but the screen is on OR the device is unlocked and the screen is on, then onNewIntent() is called instantly, and the message is shown and the notification is given.

    Anyone got any clue of whats happening?
     
  7. RNekic

    RNekic Member

    Joined:
    May 25, 2010
    Messages:
    12
    Likes Received:
    1
    I'm probably not grasping your explanation completely...but I'm still pondering your onNewIntent().

    Do you pass an intent to your doNotify() code or do you call getIntent() from within doNotify? If you are calling getIntent(), are setting the new intent in onNewIntent()? getIntent() always returns the original intent unless you update it...so you perhaps you need to do something like this in onNewIntent:

    Code (Text):
    1. @Override
    2. protected void onNewIntent(Intent intent)
    3. {
    4.     super.onNewIntent(intent);
    5.     this.setIntent(intent);
    6.         doNotify();
    7. }
     
  8. miXer

    miXer Well-Known Member This Topic's Starter

    Joined:
    Aug 12, 2010
    Messages:
    313
    Likes Received:
    51
    The problem is not in the method onNewIntent(). The problem is that onNewIntent() is not fired until the device is woken up manually. And that has to be the wake lock.

    As soon as onNewIntent() is fired, everything is fine. So the problem must be how i use the wake lock.
     
  9. RNekic

    RNekic Member

    Joined:
    May 25, 2010
    Messages:
    12
    Likes Received:
    1
    Are we talking about an Activity that implements your SMS receiver? I'm not sure I understand the context for this problem. The device sleeping would trigger the activity's onPause and it's no longer active to respond to SMS events, no? ...until you manually wake the phone which triggers onCreate/onResume to rewire the receiver?
     
  10. miXer

    miXer Well-Known Member This Topic's Starter

    Joined:
    Aug 12, 2010
    Messages:
    313
    Likes Received:
    51
    Okey.

    This scenario is when i'm using WakeLock and PowerManger

    1. I get an SMS on my phone, android sends out a broadcast which my app receives, and then starts an activity(Theme.Dialog).

    2. The dialog gives out a notification, a popup, disables the keyguard and turns the screen on

    3. If i decide not to read the message, or i'm not around the phone when the message is received, the phone goes to sleep after default timeout and enables the keyguard.

    4. I receive another SMS while the first one is still not read. Android sends out a broadcast which my app then again receives, and tries to start the same activity(Theme.Dialog), but this time the activity is allready running, so it just sends a new intent to the activity via onNewIntent(Intent). But because i probably use the WakeLock wrong, the onNewIntent(Intent) is not fired until i manually wake up the phone(push the power button), then the notification is given.


    This scenario is when just a notification is given. No wakelock or powermanger.

    1. I get an SMS on my phone, android sends out a broadcast which my app receives, and then starts an activity(Theme.Dialog).

    2. The dialog gives out a notification and a popup and the device is kept sleeping(if it was before the sms)

    3. I receive another SMS while the first one is still not read. Android sends out a broadcast which my app then again receives, and tries to start the same activity(Theme.Dialog), but this time the activity is allready running, so it just sends a new intent to the activity via onNewIntent(Intent), and the notification and popup is given and the device is kept sleeping(if it was before the sms).
     
  11. cs3vigny

    cs3vigny New Member

    Joined:
    Nov 16, 2011
    Messages:
    1
    Likes Received:
    0
    I can confirm this behavior and I am also looking for a solution that will wake the phone up without having to manually wake up the phone in this exact same scenario. Have you had any luck finding a solution?
     
  12. miXer

    miXer Well-Known Member This Topic's Starter

    Joined:
    Aug 12, 2010
    Messages:
    313
    Likes Received:
    51

Share This Page

Loading...