Light up screen when notification received android
Asked Answered
J

2

30

I have a service running for my application to send notification every hour. This is working fine as i heard a sound and a vibration every hour because of my notification but i also want that my notification light up my screen as well. But i am unable to light up my screen when notification appears.

Jam answered 9/3, 2012 at 9:42 Comment(0)
I
55
PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = pm.isScreenOn();
Log.e("screen on........", ""+isScreenOn);
if(isScreenOn==false)
{
    WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |PowerManager.ACQUIRE_CAUSES_WAKEUP |PowerManager.ON_AFTER_RELEASE,"myApp:MyLock");
    wl.acquire(10000);
    WakeLock wl_cpu = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"myApp:mycpuMyCpuLock");

    wl_cpu.acquire(10000);
}
Indentation answered 9/3, 2012 at 9:54 Comment(10)
FULL_WAKE_LOCK is deprecated now. What use instead?Noncompliance
I have the same question as Orium.Kinky
you need WakeLock permission in manifest: <uses-permission android:name="android.permission.WAKE_LOCK" />Neuropathy
isScreenOn() was deprecated in API level 20. Use isInteractive() instead.Foresaid
Thanks. You solved my issue for wake up lock. Now my app properly wake up for notifications.Pogy
FULL_WAKE_LOCK is deprecated. Use PARTIAL_WAKE_LOCK instead.Hostility
@KaiWang Are we supposed to use PARTIAL_WAKE_LOCK on both wl and wl_cpu instances?Belligerency
@Belligerency I'm not sure about it. Sorry dude :)Hostility
PARTIAL_WAKE_LOCK will not wake up a device on its own, and the Android docs state it cannot be used with ACQUIRE_CAUSES_WAKEUPWatkins
will this code come after notification code in broadcastreceiver?Televisor
K
6

Here is my solution:

createNotification(); //your implementation
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = Build.VERSION.SDK_INT >= 20 ? pm.isInteractive() : pm.isScreenOn(); // check if screen is on
if (!isScreenOn) {
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "myApp:notificationLock");
    wl.acquire(3000); //set your time in milliseconds
}

More at PowerManager

Keyser answered 11/4, 2019 at 14:41 Comment(1)
Do i need to release wakelock like wl.release() ?Selfindulgent

© 2022 - 2024 — McMap. All rights reserved.