首页 > 代码库 > 监听系统音量发生变化

监听系统音量发生变化

        AudioManager audioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE) ;
        sound = (SeekBar) findViewById(R.id.sb_sound);

        /**
	 * 注册当音量发生变化时接收的广播
	 */
	private void myRegisterReceiver(){
		mVolumeReceiver = new MyVolumeReceiver() ;
		IntentFilter filter = new IntentFilter() ;
		filter.addAction("android.media.VOLUME_CHANGED_ACTION") ;
		registerReceiver(mVolumeReceiver, filter) ;
	}
	
	/**
	 * 处理音量变化时的界面显示
	 * @author long
	 */
	private class MyVolumeReceiver extends BroadcastReceiver{
		@Override
		public void onReceive(Context context, Intent intent) {
			//如果音量发生变化则更改seekbar的位置
			if(intent.getAction().equals("android.media.VOLUME_CHANGED_ACTION")){
				int currVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC) ;// 当前的媒体音量
				sound.setProgress(currVolume) ;
			}
		}
	}



监听系统音量发生变化