Java 音量总是一样的吗?
我有下面的代码,我想在按住音量按钮和当前音量之前获得上一个音量的差异。但是,在调试时,我发现以前的卷和当前卷总是相同的: 这是我的密码:Java 音量总是一样的吗?,java,android,string,algorithm,function,Java,Android,String,Algorithm,Function,我有下面的代码,我想在按住音量按钮和当前音量之前获得上一个音量的差异。但是,在调试时,我发现以前的卷和当前卷总是相同的: 这是我的密码: package curlybrace.ruchir.ivebeenstuckfortwodays; import android.content.Context; import android.database.ContentObserver; import android.media.AudioManager; import android.os.Han
package curlybrace.ruchir.ivebeenstuckfortwodays;
import android.content.Context;
import android.database.ContentObserver;
import android.media.AudioManager;
import android.os.Handler;
/**
* Created by ruchir on 2/5/2016.
*/
public class volumeCheck extends ContentObserver {
int previousVolume;
Context context;
public volumeCheck(Context c, Handler handler) {
super(handler); //Creates a new handler
context=c; //variable context, defined earlier, is set equal to c, context of service.
AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); //retrieve an AudioManager for handling management of volume, ringer modes and audio routing.
previousVolume = audio.getStreamVolume(AudioManager.STREAM_MUSIC); //The volume that we get before the `onChange` is called
}
@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);
processVolumeChange(previousVolume, currentVolume);
}
public void processVolumeChange(int previousVolume, int currentVolume) {
MyService mService = new MyService();
mService.volumeCheck(previousVolume - currentVolume); //Method in my service
}
}
我已经花了两天的时间试图解决这个问题,但我不知道为什么这些值是相同的。请帮忙
编辑:
我的服务的
onCreate
中有以下代码:
mSettingsContentObserver = new volumeCheck(this, new Handler());
getApplicationContext().getContentResolver().registerContentObserver(android.provider.Settings.System.CONTENT_URI, true, mSettingsContentObserver);
谢谢您可能没有更改设备上的媒体卷。要验证,请转到设置->声音和通知,然后尝试更改在其中找到的媒体卷 您可能还希望在更改后更新您的
以前的卷
:
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
int currentVolume = audio.getStreamVolume(AudioManager.STREAM_MUSIC);
processVolumeChange(currentVolume);
}
public void processVolumeChange(int currentVolume) {
MyService mService = new MyService();
mService.volumeCheck(previousVolume - currentVolume); //Method in my service
previousVolume = currentVolume;
}
您可能没有更改设备上的媒体卷。要验证,请转到设置->声音和通知,然后尝试更改在其中找到的媒体卷 您可能还希望在更改后更新您的
以前的卷
:
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
int currentVolume = audio.getStreamVolume(AudioManager.STREAM_MUSIC);
processVolumeChange(currentVolume);
}
public void processVolumeChange(int currentVolume) {
MyService mService = new MyService();
mService.volumeCheck(previousVolume - currentVolume); //Method in my service
previousVolume = currentVolume;
}
您正在向哪个Uri注册此
ContentObserver
?我的服务的onCreate
中有以下代码:msettingsContentTobServer=newVolumeCheck(此,新处理程序());getApplicationContext().getContentResolver().registerContentObserver(android.provider.Settings.System.CONTENT\u URI,true,mSettingsContentObserver)
您将此ContentObserver
注册到哪个Uri?我在我的服务的onCreate
中有以下代码:mSettingsContentObserver=newvolumecheck(此,new Handler());getApplicationContext().getContentResolver().registerContentObserver(android.provider.Settings.System.CONTENT\u URI,true,mSettingsContentObserver)代码>我仍然没有从我的服务中得到我的祝酒词。我想我没有正确地调试。我在这一行上放置了一个断点:processVolumeChange(previousVolume,currentVolume)代码>。现在,当我尝试访问服务时,我的服务中出现了一个空指针异常…好的,但您现在调试时是否获得了正确的值?不,我仍然没有。我现在非常困惑。我得到了一个空指针异常,我不认为我调试正确,但我仍然没有从我的服务中得到我的祝福。我想我没有正确地调试。我在这一行上放置了一个断点:processVolumeChange(previousVolume,currentVolume)代码>。现在,当我尝试访问服务时,我的服务中出现了一个空指针异常…好的,但您现在调试时是否获得了正确的值?不,我仍然没有。我现在非常困惑。我得到一个空指针异常,我认为我调试不正确