不断为您在Android的服务量变化您在、Android

2023-09-04 09:01:42 作者:别说话、吻我

我写了这片code,这显然是有缺陷的。我该如何去创造它会不断检查成交量的变化服务?关键听众不能在服务中使用,请不要发表与音量键听众的答案。

我的code是错误的,因为我已经给假人条件while循环。有什么是条件,我的服务,以检查体积变化并不会崩溃?它不能是 isScreenOn(),因为量能边听音乐,屏幕关闭时被改变。

code

  AudioManager音频=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
    INT currentVolume = audio.getStreamVolume(AudioManager.STREAM_RING);
    INT newVolume = 0;
    而(1 == 1)
    {
        newVolume = audio.getStreamVolume(AudioManager.STREAM_RING);
        如果(currentVolume!= newVolume)
        {
            Toast.makeText(这一点,音量变化来检测!,Toast.LENGTH_LONG).show();
        }

    }
 

解决方案

如果你想看到的时候设置的变化,你可以注册一个ContentObserver的设置提供对其进行监视。例如,这是在设置应用程序的观察员:

 私人ContentObserver mVolumeObserver =新ContentObserver(mHandler){
        @覆盖
        公共无效的onChange(布尔selfChange){
            super.onChange(selfChange);
            如果(mSeekBar = NULL和放大器;!&安培;!mAudioManager = NULL){
                INT体积= mAudioManager.getStreamVolume(mStreamType);
                mSeekBar.setProgress(体积);
            }
        }
    };
 
如何在 Android 上更改您的 DNS 服务器

和它注册到观察者的设置是这样的:

 进口android.provider.Settings;
        进口android.provider.Settings.System;

        mContext.getContentResolver()。registerContentObserver(
                System.getUriFor(System.VOLUME_SETTINGS [mStreamType]),
                假的,mVolumeObserver);
 

I wrote this piece of code, it's obviously flawed. How do I go about creating a service which will constantly check for changes in volume? Key listeners cannot be used in services, please don't post an answer with volume key listeners.

My code is wrong because I've given dummy condition for the while loop. What has to be the condition for my service to check for volume change and not crash? It can't be isScreenOn() because volume can be changed while listening to music and the screen is off.

CODE

AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    int currentVolume = audio.getStreamVolume(AudioManager.STREAM_RING);
    int newVolume=0;
    while(1==1)
    {
        newVolume = audio.getStreamVolume(AudioManager.STREAM_RING);
        if(currentVolume!=newVolume)
        {
            Toast.makeText(this, "Volume change detected!", Toast.LENGTH_LONG).show();
        }

    }

解决方案

If you want to see when the setting changes, you can register a ContentObserver with the settings provider to monitor it. For example, this is the observer in the settings app:

    private ContentObserver mVolumeObserver = new ContentObserver(mHandler) {
        @Override
        public void onChange(boolean selfChange) {
            super.onChange(selfChange);
            if (mSeekBar != null && mAudioManager != null) {
                int volume = mAudioManager.getStreamVolume(mStreamType);
                mSeekBar.setProgress(volume);
            }
        }
    };

And it is registered to observer the settings like this:

        import android.provider.Settings;
        import android.provider.Settings.System;

        mContext.getContentResolver().registerContentObserver(
                System.getUriFor(System.VOLUME_SETTINGS[mStreamType]),
                false, mVolumeObserver);