Android请求ACCESS_NOTIFICATION_POLICY并将手机静音 [英] Android request ACCESS_NOTIFICATION_POLICY and mute phone

查看:1876
本文介绍了Android请求ACCESS_NOTIFICATION_POLICY并将手机静音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名初学者Android开发人员,并且手头有一个有趣的问题.我正在尝试使用

I am a beginner Android developer and I have an interesting question at hand. I am trying to mute the phone with

AudioManager audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);

但是我遇到了以下错误

java.lang.SecurityException: Not allowed to change Do Not Disturb state

现在我不明白这一点,因为我正在使用EasyPermissions( https://github.com/googlesamples/easypermissions )并请求许可

Now I do not understand this, as I am using EasyPermissions (https://github.com/googlesamples/easypermissions) and requesting the permission

Manifest.permission.ACCESS_NOTIFICATION_POLICY

但是它不要求我在应用启动时允许任何操作.我认为这是因为ACCESS_NOTIFICATION_POLICY是一种非危险的权限,因此是在安装时授予的,为此我也将其添加到了manifest.xml中

But it does not ask me to allow anything on app startup. I figured this is because ACCESS_NOTIFICATION_POLICY is a non dangerous permission and thus granted at installation time, for which I also added it to my manifest.xml as thus

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

但是它仍然无法正常工作,我的应用程序崩溃是因为它引发了不允许更改为请勿打扰状态"错误.奇怪的是,我发现我可以请求使用以下代码进入请勿打扰"屏幕

But it is still not working, my app is crashing because it throws the "Not allowed to change to Do Not Disturb state" error. Wierdly I found that I can request to go to the "Do Not Disturb access" screen with the following code

Intent intent = new Intent(
                    android.provider.Settings
                            .ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
 startActivity(intent);

我在这里做错了什么?为什么我不能要求此权限为普通权限?我是否真的必须通过意图使我的应用程序使手机静音?

What am I doing wrong here? Why cannot I request this permissions as a normal permission? Do I really have to go through the intent to allow my app to mute the phone?

推荐答案

来自 AudioManager#setRingerMode()参考:

从N开始,不允许振铃器模式调整以切换请勿打扰",除非已授予该应用请勿打扰"访问权限.

From N onward, ringer mode adjustments that would toggle Do Not Disturb are not allowed unless the app has been granted Do Not Disturb Access.

从API级别23开始,您必须在清单 AND 中声明ACCESS_NOTIFICATION_POLICY权限,然后用户需要授予您的应用访问权限以切换请勿打扰".您可以使用 NotificationManager#isNotificationPolicyAccessGranted().如果您的软件包没有访问权限,请启动ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS意图,以便用户可以授予您的应用程序访问权限.

From API level 23 and onward, you have to declare ACCESS_NOTIFICATION_POLICY permission in the manifest AND then the user needs to grant your app access to toggle Do Not Disturb. You can check if the access is granted with NotificationManager#isNotificationPolicyAccessGranted(). If your package do not have access, start an ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS intent so the user can give your app access.

NotificationManager n = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
if(n.isNotificationPolicyAccessGranted()) {
  AudioManager audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
  audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
}else{
  // Ask the user to grant access
  Intent intent = new Intent(android.provider.Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
  startActivityForResult(intent);
}

这篇关于Android请求ACCESS_NOTIFICATION_POLICY并将手机静音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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