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

Apps PowerManager and WakeLock

miXer

Android Enthusiast
Aug 12, 2010
313
51
Norway
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:
private void disableKeyGuard()
    {
        KeyguardManager km = (KeyguardManager)getSystemService(KEYGUARD_SERVICE);
        kl = km.newKeyguardLock("SMSHandler");
        kl.disableKeyguard();
        km.exitKeyguardSecurely(null);
    }

    private void turnOnScreen()
    {
        PowerManager pm = (PowerManager)getSystemService(POWER_SERVICE);
        wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP|PowerManager.SCREEN_DIM_WAKE_LOCK|PowerManager.ON_AFTER_RELEASE, "SMSHandler");
        wl.acquire();
        wl.release();
    }
 
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?
 
Upvote 0
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.
 
Upvote 0
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.
 
Upvote 0
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?
 
Upvote 0
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:
@Override
protected void onNewIntent(Intent intent)
{
	super.onNewIntent(intent);
	this.setIntent(intent);
        doNotify();
}
 
Upvote 0
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?
 
Upvote 0
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).
 
Upvote 0
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).

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?
 
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