Android N:在更新复选框首选项之前检查DND状态是否已更改 [英] Android N: checking for DND state changed before updating checkbox preference
问题描述
我的应用程序将振铃器模式设置为静音,但是使用Android N我得到了java.lang.SecurityException: Not allowed to change Do Not Disturb state
.
My app sets the ringer mode to silent but with Android N I get a java.lang.SecurityException: Not allowed to change Do Not Disturb state
.
I followed the steps from this post and the following code will open an activity which contains a toggle button for enabling the permission to change the ringer mode. This activity can be "backed" from without the enabling of the permission.
Intent intent = new Intent(
android.provider.Settings
.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
startActivity(intent);
问题是我想确保在尝试使用无提示振铃模式功能之前已授予该权限.
The problem is that I want to ensure that the permission is granted before attempting to use the silent ringer mode functionality.
我正在添加CheckBoxPreference来启用和禁用此铃声模式切换.因此,如果选中此复选框,则可以将振铃器模式设置为无声,如果没有,则不会更改振铃器模式.
I'm adding a CheckBoxPreference to enable and disable of this ringer mode toggling. So if the checkbox is checked, I can set the ringer mode to silent and if it's not, I will not change the ringer mode.
直到现在,当用户单击复选框时,我设法启动了此权限请求:
Until now I managed to start this permission request when the user clicks the checkbox:
disableSoundCheckboxPreference.setOnPreferenceChangeListener(
new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object o) {
...
// check for Android N and start the activity mentioned above
但是,仅在授予许可的情况下,启用此复选框的正确方法是什么?我想它应该与ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED
和一个侦听器有关,但是我需要一些想法来实现这一点.
But what is the correct way to enable this checkbox only if the permission was granted? I guess it should have something to do with ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED
and a listener but I would need some ideas to implement this.
谢谢!
推荐答案
我设法解决了这个问题,并将在其他人需要的时候发布答案.
I managed to solve this and will post the answer in case someone else needs it.
在这种情况下,不需要BroadcastListener.您只需要在用户首次使用OnPreferenceClickListener
A BroadcastListener is not needed in this case. You just need to request the permission when the user first clicks on the preference with an OnPreferenceClickListener
:
private void requestNotificationPolicyAccess() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && !isNotificationPolicyAccessGranted()) {
Intent intent = new Intent(android.provider.Settings.
ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
startActivity(intent);
如果用户授予许可,则当他再次单击时,将允许用户更改CheckBox
的状态.如果未授予该权限,则将继续用CheckBox
,并可能在android:summary
中写入必须首先授予该权限.您可以通过以下方式进行验证:
If the user grants the permission, when he will click again, you will allow the user to change the state of the CheckBox
. If the permission is not granted, you will continue updating the CheckBox
with "false" and maybe write in it's android:summary
that permission must be granted first. You can verify with:
private boolean isNotificationPolicyAccessGranted() {
NotificationManager notificationManager = (NotificationManager)
getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
return notificationManager.isNotificationPolicyAccessGranted();
}
这篇关于Android N:在更新复选框首选项之前检查DND状态是否已更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!