1

我正在尝试使用内容观察者来更改系统响铃音量。使用这个卷整数我想触发另一个 java 文件。

我成功使用了下面的代码,但问题是我总是得到两个日志。我试图区分previousVolume 和ChangedVolume。我不是为什么我要在更改方法中记录?这样做的好处是价值观没有改变?

这是我尝试过的代码:

public class VolumeChecker extends ContentObserver 
{
 int previousVolume;
 Context context;

public VolumeChecker(Context c, 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_RING);

    Log.e("changed", "Volume"+currentVolume);
 }
 }

并将 obersever -from 服务注册为:

Volume = new VolumeChecker(this,new handler());
getApplicationContext().getContentResolver().registerContentObserver(android.provider.Settings.System.CONTENT_URI, true, Volume );

有谁知道为什么它在 onChange 部分内记录两次?

4

0 回答 0