有没有办法在Android上收听音量变化的事件,而不仅仅是接管音量按钮?
我发现唯一有效的是这里,但它只有在音量控制消失后才能工作。
并非所有设备都有音量按钮,我需要在音量变化发生后立即捕获音量变化,而不是在音量对话框消失后。
更好的是,您可以按如下方式注册ContentObserver
:
getApplicationContext().getContentResolver().registerContentObserver(android.provider.Settings.System.CONTENT_URI, true, new ContentObserver(){...} );
您的 ContentObserver 可能如下所示:
public class SettingsContentObserver extends ContentObserver {
private AudioManager audioManager;
public SettingsContentObserver(Context context, Handler handler) {
super(handler);
audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
}
@Override
public boolean deliverSelfNotifications() {
return false;
}
@Override
public void onChange(boolean selfChange) {
int currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
Log.d(TAG, "Volume now " + currentVolume);
}
}
完成后:
getApplicationContext().getContentResolver().unregisterContentObserver(mContentObserver);
不过,有一个警告 - 有时如果快速按下大量按钮,通知似乎会延迟。
好的,现在,我所做的是使用 onKeyDown 收听音量按钮(并检查 KEYCODE_VOLUME_DOWN,KEYCODE_VOLUME_MUTE,KEYCODE_VOLUME_UP ),并使用处理程序我发布了一个新的可运行对象来检查音量级别。
另外,由于某些设备具有音量对话框,因此我根据此链接在它消失时添加了一个侦听器。
使用广播接收器VOLUME_CHANGED_ACTION然后使用音频管理器获取当前音量。
<receiver android:name="VolumeChangeReceiver" >
<intent-filter>
<action android:name="android.media.VOLUME_CHANGED_ACTION" />
</intent-filter>
</receiver>
: registerMediaButtonEventReceiver (ComponentName eventReceiver)它将组件注册为MEDIA_BUTTON意图的唯一接收方。
// in your activity.
MediaButtonReceiver receiver = new MediaButtonReceiver();
// in onCreate put
registerMediaButtonEventReceiver(receiver);
class MediaButtonReceiver implements BroadcastReceiver {
void onReceive(Intent intent) {
KeyEvent ke = (KeyEvent)intent.getExtra(Intent.EXTRA_KEY_EVENT);
if (ke .getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN) {
//action when volume goes down
}
if (ke .getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP) {
//action when volume goes up
}
}
}
//In both onStop and onPause put :
unregisterMediaButtonEventReceiver(receiver);
我们在这里所做的是定义一个处理ACTION_MEDIA_BUTTON的 BroadcastReceiver,并使用包含导致广播获取所按下内容并对其采取行动的关键事件的EXTRA_KEY_EVENT。
> 2023 年带有广播接收器
的解决方案可与音量按钮和安卓系统界面配合使用。在这个例子中,我使用BroadcastReceiver作为内部类。我正在使用与此类似的实现来更新片段中的搜索栏。
public class AFragment extends Fragment {
private Context context;
private AudioManager audioManager;
// THE STREAM TYPE YOU WANT VOLUME FROM
private final int streamType = AudioManager.STREAM_MUSIC;
private VolumeChangeListener volumeChangeListener;
private IntentFilter intentFilter;
private final String VOLUME_CHANGED_ACTION = "android.media.VOLUME_CHANGED_ACTION";
public AFragment(Context context) {
this.context = context;
this.audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
this.volumeChangeListener = new VolumeChangeListener();
this.intentFilter = new IntentFilter(VOLUME_CHANGED_ACTION);
}
@Override
public void onDestroy() {
super.onDestroy();
context.unregisterReceiver(volumeChangeListener);
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context.registerReceiver(volumeChangeListener, intentFilter);
}
private class VolumeChangeListener extends BroadcastReceiver {
// VOLUME CHANGED ACTION triggers 3 times
// Making use of increment variable to make sure to skip the extra calls
private int callCount = 0;
@Override
public void onReceive(Context context, Intent intent) {
callCount++;
if (intent.getAction().equals(VOLUME_CHANGED_ACTION)) {
if (callCount % 3 == 0) {
int SYSTEM_currentVolume = audioManager.getStreamVolume(streamType);
// DO WHATEVER YOU WANT WITH THE NEW VOLUME VALUE
}
}
}
}
}
IntentFilter android.media.VOLUME_CHANGED_ACTION可以在这里找到: Android Google Source