在状态栏中发送新通知时如何打开屏幕? [英] How to turn on screen when new notification is sent in the status bar?

查看:86
本文介绍了在状态栏中发送新通知时如何打开屏幕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我设置通知的代码,它起作用:

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

这是我的BroadcastReceiver的代码段。每当调用此BroadcastReceiver时,它都会在状态栏中显示一个通知。在调试过程中,我注意到当屏幕关闭并发出新通知时,屏幕将不会打开。有什么办法吗?无论何时发出新通知且屏幕关闭,都应将其打开一段时间。就像接收新短信一样。

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.

推荐答案

尝试以下操作:

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

这篇关于在状态栏中发送新通知时如何打开屏幕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆