我试图全局拦截音量键(屏幕关闭和应用程序外部)到目前为止我可以检测按钮按下,但我无法解决如何取消它。这是我到目前为止的代码
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;
}
}
}
我需要取消音量事件,以便音量不会改变。