获取设置状态:"Control notifications on your lock screen"

Get status of setting: "Control notifications on your lock screen"

在 Android 上,您在“通知”下设置了 "Control notifications on your lock screen",作为支持,您可以设置通知的可见性,如 notification Tutorial 中所述。

那么我的问题是,是否可以获取此设置的状态?

我想要设置状态的原因是,如果用户启用了内容隐藏,则可以为用户提供更多选项,但如果没有启用,则不会打扰他们。

是的,这是可能的。有两个常量:

Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS = "lock_screen_allow_private_notifications"
Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS = "lock_screen_show_notifications"

但是,由于这些值不是 public API 的一部分,它们将来可能会发生变化,或者可能无法在所有设备上工作。

int show_all = Settings.Secure.getInt(getContentResolver(), "lock_screen_allow_private_notifications", -1); 
int noti_enabled = Settings.Secure.getInt(getContentResolver(), "lock_screen_show_notifications", -1); 

if(show_all > 0 && noti_enabled > 0){
  // Post notification
  // ...
}