Check Android user's “when device is locked” n

2019-05-07 14:57发布

On Android L, I would like show the user a notification on the lock screen only if the user settings is set to "show all notification content", otherwise the content will be pointless and I just prefer not to show the notification at all.

Any idea how to verify in code the user notification settings?

Thanks!

2条回答
Ridiculous、
2楼-- · 2019-05-07 15:18

You need to read

Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS = "lock_screen_allow_private_notifications"

Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS = "lock_screen_show_notifications"

only if both are 1 then you need to show your notifications. But since these values are not part of public api, these might change in future, or might not work on all devices

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 noti
}
查看更多
Evening l夕情丶
3楼-- · 2019-05-07 15:20

You can't check that setting as far as I know, but your app can control the level of detail visible when its notifications are displayed over the secure lock screen. To control the visibility level, call setVisibility() (Notification.Builder.setVisibility) and specify one of these values:

VISIBILITY_PUBLIC: Shows the notification’s full content.

VISIBILITY_PRIVATE: Shows basic information, such as the notification’s icon, but hides the notification’s full content.

VISIBILITY_SECRET: Shows nothing, excluding even the notification’s icon.

When the visibility level is VISIBILITY_PRIVATE, you can also provide a redacted version of the notification content that hides personal details. For example, an SMS app might display a notification that shows "You have 3 new text messages" but hides the message content and senders. To provide this alternative notification, first create the replacement notification using Notification.Builder. When you create the private notification object, attach the replacement notification to it through the setPublicVersion() method.

Sources

查看更多
登录 后发表回答