如何在 Android 5 (Lollipop) 中抑制锁定屏幕上的通知但让它在通知区域中? [英] How to suppress notification on lock screen in Android 5 (Lollipop) but let it in notification area?

查看:20
本文介绍了如何在 Android 5 (Lollipop) 中抑制锁定屏幕上的通知但让它在通知区域中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

升级到 Android 5.0 Lollipop 后,它开始在锁定屏幕上自动显示正在进行的通知.

After upgrade to Android 5.0 Lollipop it started showing automatically ongoing notification on lock screen.

有时用户不想看到所有这些,所以他们会问开发人员如何在状态区域中显示通知,但在锁定屏幕上隐藏它们.

Sometimes users don't want to see all of them so they are asking developers how to let notification in status area but hide them on lock screen.

我发现的唯一方法是强制用户使用屏幕锁定(例如手势或 PIN)并以编程方式setVisibility()VISIBILITY_SECRET.但并非所有人都想使用屏幕锁定.

Only way I found is to force users use screen lock (eg. Gesture or PIN) and programatically setVisibility() to VISIBILITY_SECRET. But not all them want to use screen lock.

是否有任何标志(或标志组合)告诉通知:在锁定屏幕上不可见,但在通知区域中可见?

Is there any flag (or combination of flags) saying to notification: don't be visible on Lock screen but be visible in notification area?

推荐答案

使用可见性和优先级

此答案所述,您可以使用VISIBILITY_SECRET 当用户有安全锁屏时禁止在锁屏上显示通知(不仅仅是滑动或没有键盘保护)并且抑制了敏感通知.

Use visibility and priority

As covered by this answer, you can use VISIBILITY_SECRET to suppress the notification on the lock screen when the user has a secure keyguard (not just swipe or no keyguard) and has sensitive notifications suppressed.

为了涵盖其余的情况,您可以通过将通知的优先级设置为 PRIORITY_MIN 每当键盘锁存在,然后在不存在键盘锁时重置优先级.

To cover the rest of the cases, you can programmatically hide the notification from the lock screen and status bar by setting the notification's priority to PRIORITY_MIN whenever the keyguard is present and then reset the priority whenever the keyguard is absent.

final BroadcastReceiver notificationUpdateReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        NotificationManager notificationManager =
            (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

        NotificationCompat.Builder builder =
            new NotificationCompat.Builder(context, YOUR_NOTIFICATION_CHANNEL_ID)
                .setSmallIcon(R.drawable.your_icon)
                .setVisibility(NotificationCompat.VISIBILITY_SECRET);
        
        KeyguardManager keyguardManager =
            (KeyguardManager)context.getSystemService(Context.KEYGUARD_SERVICE);

        if (keyguardManager.isKeyguardLocked())
            builder.setPriority(NotificationCompat.PRIORITY_MIN);
        
        notificationManager.notify(YOUR_NOTIFICATION_ID, builder.build());
    }
};

//For when the screen might have been locked
context.registerReceiver(notificationUpdateReceiver,
    new IntentFilter(Intent.ACTION_SCREEN_OFF));

//Just in case the screen didn't get a chance to finish turning off but still locked
context.registerReceiver(notificationUpdateReceiver,
    new IntentFilter(Intent.ACTION_SCREEN_ON));

//For when the user unlocks the device
context.registerReceiver(notificationUpdateReceiver,
    new IntentFilter(Intent.ACTION_USER_PRESENT));

//For when the user changes users
context.registerReceiver(notificationUpdateReceiver,
    new IntentFilter(Intent.ACTION_USER_BACKGROUND));
context.registerReceiver(notificationUpdateReceiver,
    new IntentFilter(Intent.ACTION_USER_FOREGROUND));

这篇关于如何在 Android 5 (Lollipop) 中抑制锁定屏幕上的通知但让它在通知区域中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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