关于监听Android的静音键以及音量按键

最近项目中涉及到物理键以及静音的监听,上来就写了个BrocastReceiver,然后注册,处理。但是在测的时候发现,在不同的手机上的这块的onReceive()回调是有不一样的,以下为总结:

1.类型一:有背景音乐播放的时候 音量上下键调节的是媒体音量,否则调节的是系统音量

2.类型二:除了通过上下键调节,还需要对静音键进行监听

3.还有一个影响因素  就是多进程:会导致多次调用,这时候需要过滤进程

/**
 * 监听系统音量
 *
 * @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;
    }

}

 

注册:

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

//在不同手机适配的时候 ,最好打印手机的音量变化 以及静音模式日志 可以分析不同系统的走向进而进行适配。

你可能感兴趣的:(Android,android)