About monitoring Android's mute button and volume button

Recent projects involve physical keys and mute monitoring. I wrote a BrocastReceiver, then registered and processed it. However, during the test, it was found that the onReceive() callbacks on different mobile phones are different. The following is a summary:

1. Type 1: When there is background music playing, the volume up and down keys adjust the media volume, otherwise the system volume is adjusted

2. Type 2: In addition to adjusting through the up and down keys, you also need to monitor the mute key

3. Another influencing factor is multi-process: it will lead to multiple calls. At this time, the process needs to be filtered

/**
 * 监听系统音量
 *
 * @author by 
 */
public class VolumeReceiver extends BroadcastReceiver {

    public static final String ACTION_VOLUME_CHANGED = "android.media.VOLUME_CHANGED_ACTION";
    public static final String EXTRA_VOLUME_STREAM_TYPE = "android.media.EXTRA_VOLUME_STREAM_TYPE";
    public static final String RINGER_MODE_CHANGED_ACTION = "android.media.RINGER_MODE_CHANGED";
    private VolumeChangeListener mVolumeChangeListener;

    public void setVolumeChangeListener(VolumeChangeListener volumeChangeListener) {
        this.mVolumeChangeListener = volumeChangeListener;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        synchronized (this) {
        if (!getProcessName(context).equals("com.test.cn")) {
            return;
        }
        if (ACTION_VOLUME_CHANGED.equals(intent.getAction())
                && (intent.getIntExtra(EXTRA_VOLUME_STREAM_TYPE, -1) == AudioManager.STREAM_MUSIC)) {
            handleChangeListener(context, AudioManager.STREAM_MUSIC);
        } else if (ACTION_VOLUME_CHANGED.equals(intent.getAction())) {
            handleChangeListener(context, AudioManager.STREAM_SYSTEM);
        } else if (RINGER_MODE_CHANGED_ACTION.equals(intent.getAction())) {
            AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
            final int ringerMode = audioManager.getRingerMode();
            String silentFlag = VoiceMediaManager.getInstance(context).getSilent() ? "1" : "0";
            switch (ringerMode) {
                case AudioManager.RINGER_MODE_NORMAL:
                    //normal
                    if (silentFlag.equals("1")) {
                        mVolumeChangeListener.onVolumeChanged(0);
                    }
                    IMLog.e("TAG", "铃声");
                    break;
                case AudioManager.RINGER_MODE_VIBRATE:
                    //vibrate
                    IMLog.e("TAG", "震动");
//                        mVolumeChangeListener.onVolumeChanged(1);
                    break;
                case AudioManager.RINGER_MODE_SILENT:
                    //silent
                    IMLog.e("TAG", "静音");
                    if (silentFlag.equals("0")) {
                        mVolumeChangeListener.onVolumeChanged(1);
                    }
                    break;
            }
        }
        }

    }

    public interface VolumeChangeListener {
        /**
         * 系统媒体音量变化
         *
         * @param volume 音量大小
         */
        void onVolumeChanged(int volume);
    }

  

    int musicVoice=0;
    int systenVoice=0;
    public void handleChangeListener(Context context, int type) {
        AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        int streamVolume = 0;
        if (mAudioManager != null) {
            streamVolume = mAudioManager.getStreamVolume(type);
            if (type == AudioManager.STREAM_SYSTEM) {
                if (systenVoice==streamVolume){
                    return;
                }
                systenVoice=streamVolume;
                IMLog.e("TAG",   "系统音量" + streamVolume);
            } else if (type == AudioManager.STREAM_MUSIC) {
                if (musicVoice==streamVolume){
                    return;
                }
                musicVoice=streamVolume;
                IMLog.e("TAG",   "媒体音量" + streamVolume);
            }
            if (mVolumeChangeListener != null) {
            
                    mVolumeChangeListener.onVolumeChanged(streamVolume);
                
            }
        }
    }

    /***获取当前进程名称*/
    private String getProcessName(Context context) {
        int pid = android.os.Process.myPid();
        ActivityManager mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        if (mActivityManager != null) {
            for (ActivityManager.RunningAppProcessInfo appProcess : mActivityManager.getRunningAppProcesses()) {
                if (appProcess.pid == pid) {
                    return appProcess.processName;
                }
            }
        }
        return null;
    }

}

 

registered:

VolumeReceiver receiver = new VolumeReceiver();
volumeBroadcastReceiver.setVolumeChangeListener(this);
IntentFilter filter = new IntentFilter();
filter.addAction(VolumeReceiver.ACTION_VOLUME_CHANGED);
filter.addAction(VolumeReceiver.RINGER_MODE_CHANGED_ACTION);
mContext.registerReceiver(receiver, filter);

//When adapting different mobile phones, it is best to print the volume change and silent mode log of the mobile phone to analyze the trend of different systems and then adapt.

Guess you like

Origin blog.csdn.net/lk2021991/article/details/105515953