全局拦截和取消音量按钮按下

时间:2014-12-29 01:53:05

标签: java android events button

我试图全局拦截音量键(屏幕关闭和应用程序外部)到目前为止我可以检测按钮按下,但我无法解决如何取消它。这是我到目前为止的代码

public class SettingsContentObserver extends ContentObserver {
private static final String TAG = "SettingContent";
int previousVolume;
Context context;

public SettingsContentObserver(Context c, android.os.Handler handler) {
    super(handler);
    context = c;

    AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    previousVolume = audio.getStreamVolume(AudioManager.STREAM_MUSIC);
}

@Override
public boolean deliverSelfNotifications() {
    return super.deliverSelfNotifications();
}

@Override
public void onChange(boolean selfChange) {
    super.onChange(true);

    AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    int currentVolume = audio.getStreamVolume(AudioManager.STREAM_MUSIC);

    int delta = previousVolume - currentVolume;

    if (delta > 0) {
        Log.d(TAG, "Decreased");
        previousVolume = currentVolume;
    } else if (delta < 0) {
        Log.d(TAG, "Increased");
        previousVolume = currentVolume;
    }
}

}

我需要取消音量事件,以便音量不会改变。

0 个答案:

没有答案