Pirate
Pirate

Reputation: 545

Light up screen when notification received android

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.

Upvotes: 30

Views: 30106

Answers (2)

Jan Moravec
Jan Moravec

Reputation: 536

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

Upvotes: 6

Pradeep Sodhi
Pradeep Sodhi

Reputation: 2145

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);
}

Upvotes: 55

Related Questions