在 Android 7(API 级别 24)中,不允许我的应用静音 phone(将铃声模式设置为静音)
In Android 7 (API level 24) my app is not allowed to mute phone (set ringer mode to silent)
我有一个应用可以通过使用 AudioManager 并将铃声模式设置为无声来使 phone 静音,代码如下:
AudioManager audioManager =
(AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
try {
audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT)
} catch (Exception e) {
e.printStackTrace();
}
这适用于 Android 6,但现在使用 Android 7,我收到以下错误:
System.err: java.lang.SecurityException: Not allowed to change Do Not Disturb state
System.err: at android.os.Parcel.readException(Parcel.java:1683)
System.err: at android.os.Parcel.readException(Parcel.java:1636)
System.err: at android.media.IAudioService$Stub$Proxy.setRingerModeExternal(IAudioService.java:962)
System.err: at android.media.AudioManager.setRingerMode(AudioManager.java:1022)
System.err: at controllers.SearchResultController.mutePhone(SearchResultController.java:185)
我需要申请任何新权限才能使这项工作正常进行吗?
我查看了 Android 权限列表,但找不到任何相关的内容。
根据 Android 文档,您必须授予“请勿打扰”的访问权限。
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. See isNotificationPolicyAccessGranted().
请检查link
希望对您有所帮助!
您要查找的访问权限是:
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
您需要请求用户向您提供此特定权限。
感谢您的回答,这里有更多细节。
为了能够将铃声模式设置为静音,您必须请求访问通知策略的权限(如@ucsunil 所说)。
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
然后,检查你是否有这个权限。如果没有,请为您的应用打开 "Do Not Disturb access" 的设置:
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
&& !notificationManager.isNotificationPolicyAccessGranted()) {
Intent intent = new Intent(
android.provider.Settings
.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
startActivity(intent);
}
当您 运行 startActivity() 时,Android 打开您应用的“请勿打扰”访问设置。
让我感到困惑的是,请求此权限的方式与其他权限完全不同。
仅供参考,这里是请求权限的方式READ_CONTACTS:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
&& ActivityCompat.checkSelfPermission(activity,
Manifest.permission.READ_CONTACTS)
== PackageManager.PERMISSION_DENIED) {
ActivityCompat.requestPermissions(activity,
new String[]{ Manifest.permission.READ_CONTACTS },
REQUEST_CODE_READ_CONTACTS);
}
ACCESS_NOTIFICATION_POLICY 被视为 "normal" 权限,这意味着您不必在运行时请求权限。您只需将权限添加到 AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
然后,要使所有通知静音,您可以将中断过滤器设置为 INTERRUPTION_FILTER_NONE
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
previous_notification_interrupt_setting = notificationManager.getCurrentInterruptionFilter();
notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_NONE);
}
INTERRUPTION_FILTER_ALARMS - 仅警报
INTERRUPTION_FILTER_ALL - 普通过滤器
INTERRUPTION_FILTER_NONE - 无打扰(静音)
INTERRUPTION_FILTER_PRIORITY - 优先打扰
我用
int previous_notification_interrupt_setting = notificationManager.getCurrentInterruptionFilter();
稍后在 onPause() 或 onStop() 时重置过滤器
我已经解决了这个问题,方法是打开“设置”菜单并要求用户在 api > 23 时为其授予权限。
只要从任何地方调用 requestMutePermissions() 就可以了!
private void requestMutePermissions() {
try {
if (Build.VERSION.SDK_INT < 23) {
AudioManager audioManager = (AudioManager)getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
} else if( Build.VERSION.SDK_INT >= 23 ) {
this.requestForDoNotDisturbPermissionOrSetDoNotDisturbForApi23AndUp();
}
} catch ( SecurityException e ) {
}
}
private void requestForDoNotDisturbPermissionOrSetDoNotDisturbForApi23AndUp() {
NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
// if user granted access else ask for permission
if ( notificationManager.isNotificationPolicyAccessGranted()) {
AudioManager audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
} else{
// Open Setting screen to ask for permisssion
Intent intent = new Intent(android.provider.Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
startActivityForResult( intent, ON_DO_NOT_DISTURB_CALLBACK_CODE );
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == ON_DO_NOT_DISTURB_CALLBACK_CODE ) {
this.requestForDoNotDisturbPermissionOrSetDoNotDisturbForApi23AndUp();
}
}
我只在模拟器中遇到过这个问题。当我 运行 我手机上的应用程序时,它永远不会出现,并且按预期工作。
嗨,
如果有人在 Kotlin 中需要此权限,请在此处
var notificationManager = this.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
&& !notificationManager.isNotificationPolicyAccessGranted
) {
val intent = Intent(
Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS
)
startActivity(intent)
}
我非常感谢您的建议,这些建议可以挽救生命! :D
在 Android 设备中,您必须授予 DoNotDisturb 访问您的应用程序的权限
我有一个应用可以通过使用 AudioManager 并将铃声模式设置为无声来使 phone 静音,代码如下:
AudioManager audioManager =
(AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
try {
audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT)
} catch (Exception e) {
e.printStackTrace();
}
这适用于 Android 6,但现在使用 Android 7,我收到以下错误:
System.err: java.lang.SecurityException: Not allowed to change Do Not Disturb state
System.err: at android.os.Parcel.readException(Parcel.java:1683)
System.err: at android.os.Parcel.readException(Parcel.java:1636)
System.err: at android.media.IAudioService$Stub$Proxy.setRingerModeExternal(IAudioService.java:962)
System.err: at android.media.AudioManager.setRingerMode(AudioManager.java:1022)
System.err: at controllers.SearchResultController.mutePhone(SearchResultController.java:185)
我需要申请任何新权限才能使这项工作正常进行吗?
我查看了 Android 权限列表,但找不到任何相关的内容。
根据 Android 文档,您必须授予“请勿打扰”的访问权限。
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. See isNotificationPolicyAccessGranted().
请检查link
希望对您有所帮助!
您要查找的访问权限是:
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
您需要请求用户向您提供此特定权限。
感谢您的回答,这里有更多细节。
为了能够将铃声模式设置为静音,您必须请求访问通知策略的权限(如@ucsunil 所说)。
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
然后,检查你是否有这个权限。如果没有,请为您的应用打开 "Do Not Disturb access" 的设置:
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
&& !notificationManager.isNotificationPolicyAccessGranted()) {
Intent intent = new Intent(
android.provider.Settings
.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
startActivity(intent);
}
当您 运行 startActivity() 时,Android 打开您应用的“请勿打扰”访问设置。
让我感到困惑的是,请求此权限的方式与其他权限完全不同。
仅供参考,这里是请求权限的方式READ_CONTACTS:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
&& ActivityCompat.checkSelfPermission(activity,
Manifest.permission.READ_CONTACTS)
== PackageManager.PERMISSION_DENIED) {
ActivityCompat.requestPermissions(activity,
new String[]{ Manifest.permission.READ_CONTACTS },
REQUEST_CODE_READ_CONTACTS);
}
ACCESS_NOTIFICATION_POLICY 被视为 "normal" 权限,这意味着您不必在运行时请求权限。您只需将权限添加到 AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
然后,要使所有通知静音,您可以将中断过滤器设置为 INTERRUPTION_FILTER_NONE
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
previous_notification_interrupt_setting = notificationManager.getCurrentInterruptionFilter();
notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_NONE);
}
INTERRUPTION_FILTER_ALARMS - 仅警报
INTERRUPTION_FILTER_ALL - 普通过滤器
INTERRUPTION_FILTER_NONE - 无打扰(静音)
INTERRUPTION_FILTER_PRIORITY - 优先打扰
我用
int previous_notification_interrupt_setting = notificationManager.getCurrentInterruptionFilter();
稍后在 onPause() 或 onStop() 时重置过滤器
我已经解决了这个问题,方法是打开“设置”菜单并要求用户在 api > 23 时为其授予权限。
只要从任何地方调用 requestMutePermissions() 就可以了!
private void requestMutePermissions() {
try {
if (Build.VERSION.SDK_INT < 23) {
AudioManager audioManager = (AudioManager)getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
} else if( Build.VERSION.SDK_INT >= 23 ) {
this.requestForDoNotDisturbPermissionOrSetDoNotDisturbForApi23AndUp();
}
} catch ( SecurityException e ) {
}
}
private void requestForDoNotDisturbPermissionOrSetDoNotDisturbForApi23AndUp() {
NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
// if user granted access else ask for permission
if ( notificationManager.isNotificationPolicyAccessGranted()) {
AudioManager audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
} else{
// Open Setting screen to ask for permisssion
Intent intent = new Intent(android.provider.Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);
startActivityForResult( intent, ON_DO_NOT_DISTURB_CALLBACK_CODE );
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == ON_DO_NOT_DISTURB_CALLBACK_CODE ) {
this.requestForDoNotDisturbPermissionOrSetDoNotDisturbForApi23AndUp();
}
}
我只在模拟器中遇到过这个问题。当我 运行 我手机上的应用程序时,它永远不会出现,并且按预期工作。
嗨, 如果有人在 Kotlin 中需要此权限,请在此处
var notificationManager = this.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
&& !notificationManager.isNotificationPolicyAccessGranted
) {
val intent = Intent(
Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS
)
startActivity(intent)
}
我非常感谢您的建议,这些建议可以挽救生命! :D
在 Android 设备中,您必须授予 DoNotDisturb 访问您的应用程序的权限