【问题标题】:How to turn on screen when new notification is sent in the status bar?在状态栏中发送新通知时如何打开屏幕?
【发布时间】:2020-10-28 05:21:00
【问题描述】:

这是我设置通知的代码,它有效:

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

【问题讨论】:

    标签: android broadcastreceiver alarmmanager


    【解决方案1】:

    试试这个:

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

    【讨论】:

    • 还要在清单中的权限中添加
    【解决方案2】:
    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
    }
    

    【讨论】:

    • 请提供解释(可以是简短的)以附上您的答案,以便将来其他人可以使用它。
    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-05
      • 1970-01-01
      • 2020-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多