【问题标题】:Silent mode on Android 7Android 7 上的静音模式
【发布时间】:2017-02-21 09:04:28
【问题描述】:

有没有办法在android 7上将铃声模式设置为静音

if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M) {
    amanager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
}

setRingerMode 适用于 Android 6 及更低版本。 来自文档:

“从 N 开始,铃声模式调整将切换不 除非应用已被授予请勿打扰,否则不允许打扰 访问。”

有没有什么方法可以在不打开“请勿打扰访问设置”的情况下通过系统应用程序或在构建平台代码(例如此应用程序具有请勿打扰权限等时允许)来实现此目的?

【问题讨论】:

    标签: android android-source android-7.0-nougat


    【解决方案1】:

    这是我的要点,可以帮助您解决问题

    我想使用 AudioManager 在静音模式和正常模式之间切换手机,但它不适用于 android N 及更高版本。 cmets中的更多详细信息。注意:我自己是初学者,所以这绝不是专家级代码。我之所以分享它,是因为我发现这方面的帮助很少。

        package com.junaidaziz.keepitsilent;
    
    import android.app.NotificationManager;
    import android.content.*;
    import android.media.AudioManager;
    import android.os.Build;
    import android.util.Log;
    import android.widget.Toast;
    
    
    /*
     * Created by junaidaziz on 11/13/17.
     */
    
    public class ToggleSoundMode extends android.content.BroadcastReceiver {
    
        Context context;
        static AudioManager audio;
    
        @Override
        public void onReceive(Context context, Intent intent) {
            this.context = context;
            audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    
            Log.d("BroadCast: ", " Recieved");
    
            toggleSoundMode();
    
    //        Intent background = new Intent(context, BackgroundService.class);
    //        context.startService(background);
        }
    
        public void toggleSoundMode(){
            Log.d("BroadCast: ", "toggleSoundMode");
    
            //if the OS version is lower than Nougat
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
                handleAudioManager();
            }
    
            //if the OS version is Nougat or above
            else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
    
                //first we need to get the current Notification Interruption Filter
                NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                int currentMode = notificationManager.getCurrentInterruptionFilter();
    
                //Just for debugging
                Log.d("BroadCast: ", "currentmode " + currentMode);
                Log.d("BroadCast: ", "case 1: " + NotificationManager.INTERRUPTION_FILTER_ALARMS);
                Log.d("BroadCast: ", "case 2: " + NotificationManager.INTERRUPTION_FILTER_ALL);
                Log.d("BroadCast: ", "case 3: " + NotificationManager.INTERRUPTION_FILTER_NONE);
                Log.d("BroadCast: ", "case 4: " + NotificationManager.INTERRUPTION_FILTER_PRIORITY);
                Log.d("BroadCast: ", "case 5: " + NotificationManager.INTERRUPTION_FILTER_UNKNOWN);
    
                //Make the change to the notification Interruption filter First....then change the Audio Settings
                switch (currentMode){
                    case NotificationManager.INTERRUPTION_FILTER_ALL: {
                        notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_NONE);  //Toggle On the Do not disturb Mode
                        break;
                    }
                    case NotificationManager.INTERRUPTION_FILTER_UNKNOWN: {
                        notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_ALL);  //Toggle the normal mode
                        handleRingVolume(); //With the normal interruption filter tuned on, we only need to handle the volumes.
                        break;
                    }
                    case NotificationManager.INTERRUPTION_FILTER_ALARMS: {
                        notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_ALL);
                        handleRingVolume();
                        break;
                    }
                    case NotificationManager.INTERRUPTION_FILTER_NONE: {
                        notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_ALL);
                        handleRingVolume();
                        break;
                    }
                    case NotificationManager.INTERRUPTION_FILTER_PRIORITY: {
                        notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_ALL);
                        handleRingVolume();
                        break;
                    }
                }
            }
        }
    
        public void handleAudioManager(){
    
            //This part of code is pretty self explanatory.
    
            int currentMode = audio.getRingerMode();
    
            Log.d("BroadCast: ", "currentmode " + currentMode);
            Log.d("BroadCast: ", "case 1: " + AudioManager.RINGER_MODE_NORMAL);
            Log.d("BroadCast: ", "case 2: " + AudioManager.RINGER_MODE_VIBRATE);
    
            switch (currentMode) {
                case AudioManager.RINGER_MODE_NORMAL: {
                    audio.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
                    Log.d("BroadCast: ", "case: RINGER MODE NORMAL");
                    Toast.makeText(context, "Vibration Activated", Toast.LENGTH_LONG).show();
                    break;
                }
                case AudioManager.RINGER_MODE_VIBRATE: {
                    Log.d("BroadCast: ", "case; RINGER MODE VIBRATE");
                    audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                    handleRingVolume();
                    Toast.makeText(context, "Normal mode Activated", Toast.LENGTH_LONG).show();
                    break;
                }
                case AudioManager.RINGER_MODE_SILENT: {
                    Log.d("BroadCast: ", "case; RINGER MODE SILENT");
                    audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                    handleRingVolume();
                    Toast.makeText(context, "Normal mode Activated", Toast.LENGTH_LONG).show();
                    break;
                }
            }
        }
    
        public void handleRingVolume(){
            audio.setStreamVolume(AudioManager.STREAM_RING, audio.getStreamMaxVolume(AudioManager.STREAM_RING), 0);
            audio.setStreamVolume(AudioManager.STREAM_NOTIFICATION, audio.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION), 0);
            audio.setStreamVolume(AudioManager.STREAM_SYSTEM, audio.getStreamMaxVolume(AudioManager.STREAM_SYSTEM), 0);
            audio.setStreamVolume(AudioManager.STREAM_MUSIC, audio.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);
            audio.setStreamVolume(AudioManager.STREAM_ALARM, audio.getStreamMaxVolume(AudioManager.STREAM_ALARM), 0);
            Toast.makeText(context, "Max volume", Toast.LENGTH_LONG).show();
        }}
    

    在阅读了许多资源并测试了不同的方法后,我得出结论,在 Android Nougat 及更高版本上,通知管理器的请勿打扰模式以某种方式覆盖了与音频和通知相关的所有内容。

    因此,如果手机处于“请勿打扰”模式,则您无法更改声音设置或进行任何违反“请勿打扰”模式的更改。因此,如果您想让手机重新进入振铃模式,您首先需要更改 Notification InterruptionFiler。更改后,您也可以更改音量设置。这也体现在,如果开启勿扰模式,Android 将不允许您更改手机的音量,即使使用物理音量按钮也是如此。

    要使此代码正常工作,您还需要拥有以下两个权限。

    &lt;uses-permission android:name="android.permission.WRITE_SETTINGS"/&gt;

    &lt;uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY"/&gt;

    【讨论】:

    • 嗨 Junaid,即使我正在尝试同样的事情,但在来电时,我想打开铃声,但它根本不响。在 UI 中,它会增加音量并删除 dnd 的图标,但不会产生实际的声音。你遇到过这样的情况吗?
    • 根据您的描述,我猜想这也是我面临的时间问题。基本上以我的经验,电话进来,触发广播监听器并打开声音......但是来电仍然以某种方式注册在静音频道上......我不知道它是如何工作的,但就是这样我经历过……下一个来电应该有声音。我不必处理这个问题,因为我需要下一个来电时的声音......所以我没有任何可靠的解决方案。
    【解决方案2】:

    在清单中添加此权限

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

    并使用此代码弹出通知政策访问盛大

    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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-12
      • 1970-01-01
      • 2012-01-12
      • 1970-01-01
      • 2014-09-26
      相关资源
      最近更新 更多