Android 7上的静音模式

时间:2017-02-21 09:04:29

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

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

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

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

  

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

有没有办法实现这个目的,如果没有通过意图打开“请勿打扰访问设置”这样的系统应用程序或允许建立平台代码,如此应用程序不打扰权限等?

2 个答案:

答案 0 :(得分:0)

我的主旨是帮助解决问题

我想使用AudioManager在静音和普通模式之间切换手机,但它在Android N及更高版本上无效。评论中的更多细节。注意:我自己是初学者,所以这绝不是专家级代码。我分享它是因为我找不到这方面的帮助。

    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 Manager的“请勿打扰模式”会以某种方式覆盖与音频和通知相关的所有内容。

因此,如果手机处于“请勿打扰”模式,则无法更改声音设置或进行任何违反“请勿打扰”模式的更改。因此,如果您想将手机重新置于振铃模式,首先需要更改Notification InterruptionFiler。一旦改变了,你也可以改变音量设置。事实证明,如果打开“请勿打扰”模式,即使使用物理音量按钮,Android也不会让您更改手机上的音量。

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

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

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

答案 1 :(得分:0)

在清单中添加此权限

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