2

这是我设置通知的代码,它可以工作:

@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 时,它都会在状态栏中显示通知。在调试过程中,我注意到当屏幕关闭并发出新通知时,屏幕不会打开。有没有办法做到这一点?每当发出新通知并且屏幕关闭时,它应该打开一段时间。模拟接收新短信。

4

3 回答 3

4

尝试这个:

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Tag");
wl.acquire(3000);
wl.release();
于 2013-05-15T09:53:10.883 回答
1
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
}
于 2020-10-27T19:21:06.477 回答
0

实际上,如果您只想在收到通知时激活屏幕,请在创建通知后使用此代码:

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();
于 2017-06-22T18:11:09.913 回答