在Android 7(API级别24)中,不允许我的应用静音手机(将铃声模式设置为静音) [英] In Android 7 (API level 24) my app is not allowed to mute phone (set ringer mode to silent)

查看:297
本文介绍了在Android 7(API级别24)中,不允许我的应用静音手机(将铃声模式设置为静音)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,该应用程序通过使用AudioManager并设置振铃器模式以使该代码静音而使电话静音:

I have an app that mutes the phone by using AudioManager and setting ringer mode to silent with this code:

AudioManager audioManager = 
    (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
try {
    audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT)
} catch (Exception e) {
    e.printStackTrace();
}

这适用于Android 6,但现在适用于Android 7,出现以下错误:

This works with Android 6, but now with Android 7, I get the following error:

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)

要进行这项工作,是否需要任何新的权限?

Are there any new permissions I need to ask for to make this work?

我浏览了Android权限列表,但找不到任何相关的内容.

I looked through the Android permissions list, but couldn't find any that seemed relevant.

推荐答案

感谢您的回答,这里有一些详细信息.

Thanks for your answers, here is a little more detail.

要想将铃声模式设置为静音,您必须征得访问通知策略的许可(例如@ucsunil所说).

To be able to set ringer mode to silent, you must ask permission to access notification policy (like @ucsunil said).

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

然后,检查您是否具有此权限.如果不这样做,请为您的应用打开请勿打扰访问"设置:

Then, check if you have this permission. If you do not, open the settings for "Do Not Disturb access" for your app:

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会为您的应用打开请勿打扰"访问设置.

When you run startActivity(), Android opens the Do Not Disturb access settings for your app.

让我感到困惑的是,请求此权限的方式与其他权限完全不同.

What confused me, was that the way to ask for this permission is completely different from other permissions.

仅供参考,这是请求权限的方法READ_CONTACTS:

Just for reference, here is the way to ask for permission 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);
}

这篇关于在Android 7(API级别24)中,不允许我的应用静音手机(将铃声模式设置为静音)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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