我正在尝试全局覆盖音量调高按钮,我已经设法使用下面的代码。问题是,当我这样做时,我不希望音量发生变化。我查看了内容观察器上的文档,发现了这种方法" dispatchChange"。我可以用它来取消音量变化吗?
package com.kuzoncode.pressed;
import android.content.Context;
import android.database.ContentObserver;
import android.media.AudioManager;
import android.nfc.Tag;
import android.util.Log;
import java.util.logging.Logger;
/**
* Created by Bradley on 29/12/2014.
*/
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(selfChange);
AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
int currentVolume = audio.getStreamVolume(AudioManager.STREAM_MUSIC);
//audio.setStreamVolume(AudioManager.STREAM_MUSIC, previousVolume, previousVolume);
int delta = previousVolume - currentVolume;
if (delta > 0) {
Log.d(TAG, "Decreased");
previousVolume = currentVolume;
} else if (delta < 0) {
Log.d(TAG, "Increased");
previousVolume = currentVolume;
}
}
}