NotificationManager所需的权限 [英] Permissions needed for NotificationManager

查看:794
本文介绍了NotificationManager所需的权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下方法将铃声"设置为静音",并且不要将其设置为优先"

AudioManager myAudioMgr = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
NotificationManager myNOtificationMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

myAudioMgr.setRingerMode(AudioManager.RINGER_MODE_SILENT);
myNOtificationMgr.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_PRIORITY);

我不断收到安全错误

java.lang.RuntimeException:无法启动接收器MyBroadcastReceiver:java.lang.SecurityException:通知策略访问被拒绝

我已将访问通知策略"权限添加到清单文件中

<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />

我还缺少其他权限吗?

解决方案

用户似乎需要通过设置屏幕明确授予对应用的许可,以便应用通过通知api来控制优先级/静音.我假设您正在为此使用notificationManager类.一些可能对您有帮助的链接:

https://developer.android.com/reference/android/provider/Settings.html#ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS

https://developer.android.com/reference/android/app/NotificationManager.html#ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED

我认为,基本上,您需要做的就是将用户定向到显示请勿打扰访问设置",并让他启用通知管理选项(例如,您对模拟位置的操作类似)

示例代码:

startActivityForResult(new Intent(android.settings.NOTIFICATION_POLICY_ACCESS_SETTINGS), 0);

希望这会有所帮助.

I'm trying to set the Ringer to Silent and Do not Disturb to Priority Only using the following

AudioManager myAudioMgr = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
NotificationManager myNOtificationMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

myAudioMgr.setRingerMode(AudioManager.RINGER_MODE_SILENT);
myNOtificationMgr.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_PRIORITY);

I keep getting a security error

java.lang.RuntimeException: Unable to start receiver MyBroadcastReceiver: java.lang.SecurityException: Notification policy access denied

I've added the Access Notification Policy permission to my Manifest file

<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />

Am I missing an additional permission?

解决方案

It seems like the user needs to explicity grant the permission to app via the settings screen in order for the app to manipulate the priority/silent via the notifications api. I am assuming that you are using the notificationManager class for this. Some links that might help you:

https://developer.android.com/reference/android/provider/Settings.html#ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS

https://developer.android.com/reference/android/app/NotificationManager.html#ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED

I think essentially what you need to do is direct the user to the "Show Do Not Disturb access settings" and have him enable the option for notification management (Something similar for what would you do for mock location for example)

Sample code:

startActivityForResult(new Intent(android.settings.NOTIFICATION_POLICY_ACCESS_SETTINGS), 0);

Hope this helps.

这篇关于NotificationManager所需的权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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