听成交量变动对Android的事件成交量、变动、事件、Android

由网友(上课昏昏欲睡下课精神百倍)分享简介:有没有什么办法,听取他们对Android的体积变化的情况下,没有正当接管音量按钮?这工作我发现的唯一一件事就是here,但它只有在音量控制已经消失了。不是所有的设备都有音量按钮,我需要尽快发生捕获量的变化,而不是消失了音量对话框后。解决方案 更​​好,你可以注册一个 ContentObserver 如下:getApp...

有没有什么办法,听取他们对Android的体积变化的情况下,没有正当接管音量按钮?

这工作我发现的唯一一件事就是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);
    }
}
 
地球自转一直是恒定的吗

在完成的:

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.

阅读全文

相关推荐

最新文章