How to turn on screen when new notification is sent in the status bar?

牧云@^-^@ 提交于 2019-12-11 02:31:47

问题


This is my code in setting up a notification and it works:

@Override
    public void onReceive(Context context, Intent intent) {

        category = (String) intent.getExtras().get("CATEGORY");
        notes = (String) intent.getExtras().get("NOTES");

        PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
                new Intent(context, MainActivity.class), 0);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                context).setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle(category).setContentText(notes);

        mBuilder.setContentIntent(contentIntent);
        mBuilder.setDefaults(Notification.DEFAULT_SOUND);
        mBuilder.setAutoCancel(true);

        NotificationManager mNotificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(1, mBuilder.build());       
    }

This is a code snippet from my BroadcastReceiver. Whenever this BroadcastReceiver is called it displays a notification in the status bar. During my debugging I noticed that when the screen is turned off and a new notification is made, the screen won't turn on. Is there any way to do this? That whenever a new notification is made and the screen is off, it should be turned on for a duration of time. Simulating like receiving a new sms.


回答1:


try this:

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Tag");
wl.acquire(3000);
wl.release();



回答2:


Actually, if you only want active the screen when receive a notification, use this code after create the notification:

PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "Tag");
wakeLock.acquire();
wakeLock.release();


来源:https://stackoverflow.com/questions/15768419/how-to-turn-on-screen-when-new-notification-is-sent-in-the-status-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!