简体   繁体   中英

Turn the screen backlight on when my application receive notification

I need to turn the screen backlight on when my application receive notification. I get the sound, LED light on and vibration but the screen stays off.

I tried using PowerManager:

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
wl.acquire();
..screen will stay on during this section..
wl.release();

But without result.

The notification code looks like:

public void showNotification( String contentTitle, String contentText ) {
   int icon = R.drawable.notification;
   long when = System.currentTimeMillis();



   Notification notification = new Notification(icon, contentTitle, when);


   notification.flags |= Notification.FLAG_AUTO_CANCEL;
   notification.defaults |= Notification.DEFAULT_LIGHTS;




   Intent notificationIntent = new Intent(this, myapp.class);
   Context context = this.getApplicationContext();


   PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
   notification.setLatestEventInfo(this, contentTitle, contentText, contentIntent);

   NotificationManager nm = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);




   Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
   v.vibrate(300);

   notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.sonar_ping);

   notification.flags |= Notification.FLAG_SHOW_LIGHTS;
   notification.ledARGB = 0x00000000;
   notification.ledOnMS = 0;
   notification.ledOffMS = 0;

   nm.notify(1, notification);




}  

How to turn on the screen when the notifications appear in the status bar? What do I need to add in this code to achieve that?

Thanks for any advice and help.

Try this

PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |
            PowerManager.ACQUIRE_CAUSES_WAKEUP |
            PowerManager.ON_AFTER_RELEASE, "WakeLock");
    wakeLock.acquire();

and don`t forget to release it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM