听成交量变动对Android的事件 [英] Listen to volume changes events on Android
问题描述
有没有什么办法,听取他们对Android的体积变化的情况下,没有正当接管音量按钮?
这工作我发现的唯一一件事就是<一个href="http://stackoverflow.com/questions/6896746/android-is-there-a-broadcast-action-for-volume-changes">here,但它只有在音量控制已经消失了。
不是所有的设备都有音量按钮,我需要尽快发生捕获量的变化,而不是消失了音量对话框后。
更好,你可以注册一个 ContentObserver
如下:
getApplicationContext().getContentResolver().registerContentObserver(android.provider.Settings.System.CONTENT_URI,如此,新ContentObserver(){...});
您ContentObserver可能是这样的:
公共类SettingsContentObserver扩展ContentObserver {
私人AudioManager audioManager;
公共SettingsContentObserver(上下文的背景下,处理程序处理){
超(处理);
audioManager =(AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
}
@覆盖
公共布尔deliverSelfNotifications(){
返回false;
}
@覆盖
公共无效的onChange(布尔selfChange){
INT currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
Log.d(TAG卷现在的+ currentVolume);
}
}
在完成的:
<$p$p><$c$c>getApplicationContext().getContentResolver().unregisterContentObserver(mContentObserver);
一个警告,虽然 - 有时通知似乎被推迟,如果有大量的按钮presses迅速
。Is there any way to listen to the event of volume change on Android, without just taking over the volume buttons?
The only thing I've found that works is here, but it works only after the volume control has disappeared.
Not all devices have volume buttons, and I need to capture the volume changes as soon as they occur, and not after the volume dialog is gone.
Better, you can register a ContentObserver
as follows:
getApplicationContext().getContentResolver().registerContentObserver(android.provider.Settings.System.CONTENT_URI, true, new ContentObserver(){...} );
Your ContentObserver might look like this:
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);
}
}
When done:
getApplicationContext().getContentResolver().unregisterContentObserver(mContentObserver);
One caution, though - sometimes the notifications seem to be delayed if there are lots of button presses quickly.
这篇关于听成交量变动对Android的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!